#(Solved) Tower Defense game Attack Animation
44 messages · Page 1 of 1 (latest)
It is just that he is missing an object and is trying to wait for it
** You are now Level 2! **
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)
local maxDistance = 20
local nearestTarget = nil
for i, target in ipairs(workspace.Mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
return nearestTarget
end
function tower.Attack(newTower)
local target = FindNearestTarget(newTower)
if target then
target.Humanoid:TakeDamage(25)
end
wait(1)
AnimateTowerEvent:FireAllClients(newTower, "Attack")
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)
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
tower 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)
if AnimationObject then
local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator", humanoid)
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 == "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(tower, animName)
PlayAnimation(tower, animName)
end)
Animation script
it can find idle animation
i would like to thank you
its thanks to you that now im really mad at people that doesnt format their code
so i coded i python script that will format it for you
and make it a discord bot
as you can see it works pretty well
now i just need to select the code snippets when it detects lines with lua, then delete your fucking ugly code and replace it by a real code
if you are a dev, dont you want this feature ?
What feature
do you enjoy seing unformatted code or dont you care ?
if you dont care, are you a dev ?
What u mean by unformatted code
this
Ok its shit
here he formatted well cause its a good man after all
yes so now ill dev a bot since im lazy to not be able to help people that doesnt format their code
lol
bro chill out im newbie and im trying to make my first game
no its ok it was a joke
im not actually mad at you
everyone does mistake, and im sorry if you have been offended it was not my intention
nah its okay
and really i need to thank you
cause you had the perfect code for me to test
np
@knotty dagger so do you have any idea how to fix it?