#attack animations infinity yeld position tower defense game

8 messages · Page 1 of 1 (latest)

woven wolf
#

animations local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events")
local AnimateTowerEvent = events:WaitForChild("AnimateTower")

local function SetAnimation(object, animName)
    local humanoid = object:WaitForChild("Humanoid")
    local animationsFolder = object:WaitForChild("Animations")

    if humanoid and animationsFolder then
        local AnimationObject = animationsFolder:WaitForChild(animName)-- tutaj sprawdzic
        task.wait()

        if AnimationObject then
            local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
            
            local playingTrack = animator:GetPlayingAnimationTracks()
for i, track in pairs(playingTrack) do
    if track.Name == animName then
        return track
    end
end
            
            local AnimationTrack = animator:LoadAnimation(AnimationObject)
            return AnimationTrack

        end
    end
end

local function PlayAnimation(object, animName)
    local AnimationTrack = SetAnimation(object, animName)
    
    if AnimationTrack then
        if animName == "Attack" then
            AnimationTrack:Play(1, 1, 0.5)
        end
        
        if animName == "Idle" then
             AnimationTrack:Play(1, 1, 0.15)
        end
        if animName ~= "Idle" then
            AnimationTrack:Play()
        end
    else
    
        warn("Animation Track Doesnt Exist")
    end

end




workspace.Mobs.ChildAdded:Connect(function(object)
    PlayAnimation(object, "Walk")
end)

workspace.Towers.ChildAdded:Connect(function(object)
    PlayAnimation(object, "Idle")
end)

AnimateTowerEvent.OnClientEvent:Connect(function(object, animName)
        PlayAnimation(object, animName)
    end)
#

tower module script

local PhysicServie = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local events = ReplicatedStorage:WaitForChild("Events")
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
local AnimateTowerEvent = events:WaitForChild("AnimateTower")



local tower = {}

function FindNearestTarget(newTower, range)
    local nearestTarget = nil

    for i, target in ipairs(workspace.Mobs:GetChildren()) do
        local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
        if distance < range then
            nearestTarget = target
            range = distance
        end
    end    
    return nearestTarget
end


function tower.Attack(newTower)
    local config = newTower.Config
    local target = FindNearestTarget(newTower, config.Range.Value)
    if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
        
        newTower.PrimaryPart.CFrame =  CFrame.new(newTower.PrimaryPart.Position, target.HumanoidRootPart.Position
            
        )
        
        AnimateTowerEvent:FireAllClients(newTower, "Attack") -- Attack Animation
        task.wait(0.2)
        target.Humanoid:TakeDamage(config.Damage.Value)
        
        task.wait(config.Cooldown.Value)
        
    end
task.wait(0.1)
    
    tower.Attack(newTower)
end

function tower.Spawn(player, name, cframe)
    local TowerExist = ReplicatedStorage.Towers:FindFirstChild(name)
    
    if TowerExist then
            local newTower = TowerExist:Clone()
        newTower.HumanoidRootPart.CFrame = cframe
        newTower.Parent = workspace.Towers
        newTower.HumanoidRootPart:SetNetworkOwner(nil)
            
        local CFrame = Instance.new("CFrameValue")
        CFrame = Vector3.new(math.huge, math.huge, math.huge)
        CFrame = newTower.HumanoidRootPart.CFrame

            
            
        for i, object in ipairs(newTower:GetDescendants()) do
                if object:IsA("Part") then
                    object.CollisionGroup = "Tower"
                end
            end
      coroutine.wrap(tower.Attack)(newTower)
    else    
        warn("Requested tower does not exist", name)
        
    end
end

SpawnTowerEvent.OnServerEvent:Connect(tower.Spawn)

return tower