Hello everyone! I’m currently having some trouble with some scripts i had done as a commission. Basically the scripts handle the attack animations used in my game. The problem is that whenever im click my mouse to attack an enemy the animations play at random instead of 1-5 in order. Any help would be greatly appreciated. I think the issue is coming from the server script which is randomly choosing a number every time i click my mouse.
Server script:
local RaycastHitbox = require(script.Parent.RaycastHitboxV4)
local Tool = script.Parent.Parent
local attackEvent = Tool.Events:WaitForChild("Attack")
local returnEvent = Tool.Events:WaitForChild("ReturnData")
local function check(char)
for i, v in ipairs(char:GetChildren()) do
if v:IsA("Tool") and Tool == v then
return true
end
end
end
--The index is the reference which is the randomly chosen number, the first value in the table is the animation length
local animationIndex = {
["1"] = .2,
["2"] = .2,
["3"] = .2,
["4"] = .2,
["5"] = .2
}
function OnActivation(player)
local chosenNum = tostring(math.random(1,5))
returnEvent:FireClient(player, chosenNum)
if check(player.Character) then
Tool.Handle.Trail.Enabled = true
--task.wait("x amoutn of time") this is so that the sound of the slash is linked up with the slash animation
if check(player.Character) then
task.wait(animationIndex[chosenNum])
Tool.Handle.Trail.Enabled = false
end
end
end
local db = false
attackEvent.OnServerEvent:Connect(function(player)
if db == false then
db = true
OnActivation(player)
task.wait(0.4)
db = false
end
end)
client script:
local player = game:GetService("Players").LocalPlayer
local Tool = script.Parent.Parent
local hits = {}
local RaycastHitbox = require(Tool.Scripts:WaitForChild("RaycastHitboxV4"))
local hum = player.Character:WaitForChild("Humanoid")
local Animator = hum:WaitForChild("Animator")
local idleAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("Idle"))
local equipAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("equip"))
local unequipAnimation = Animator:LoadAnimation(Tool.Animations:WaitForChild("unequip"))
local swing1Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing1"))
local swing2Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing2"))
local swing3Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing3"))
local swing4Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing4"))
local swing5Animation = Animator:LoadAnimation(Tool.Animations:WaitForChild("swing5"))
local attackEvent = Tool.Events:WaitForChild("Attack")
local returnEvent = Tool.Events:WaitForChild("ReturnData")
local slashSound = Tool.Sounds:WaitForChild("SwordSlash")
local sheathSound = Tool.Sounds:WaitForChild("sheath")
local unsheathSound = Tool.Sounds:WaitForChild("Unsheath")
local hitSound = Tool.Sounds:WaitForChild("hitSound")
local firebrandHitbox = RaycastHitbox.new(script.Parent)
firebrandHitbox:LinkAttachments(Tool.Handle.Attachment1, Tool.Handle.Attachment2)
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {player.Character}
RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
firebrandHitbox.RaycastParams = RaycastParams
firebrandHitbox.Visualizer = false
firebrandHitbox.DetectionMode = RaycastHitbox.DetectionMode.PartMode
firebrandHitbox.OnHit:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if table.find(hits, hit.Parent.Name) then return end
local character = hit.Parent
local hum = character.Humanoid
hitSound:Play()
table.insert(hits, character.Name)
game.ReplicatedStorage.InflictDamage:FireServer(Tool.Name, hit.Parent.Name)
delay(0.50, function()
table.remove(hits, table.find(hits, character.Name))
end)
elseif hit.Parent.Parent == workspace.Resources then
hitSound:Play()
game.ReplicatedStorage.BreakResource:FireServer(hit.Parent.Name)
end
idleAnimation:Stop()
unequipAnimation:Play()
end)
Tool.Equipped:Connect(function()
unsheathSound:Play()
equipAnimation:Play()
equipAnimation.Stopped:Wait()
idleAnimation:Play()
end)
Tool.Unequipped:Connect(function()
sheathSound:Play()
idleAnimation:Stop()
unequipAnimation:Play()
end)
Tool.Activated:Connect(function()
attackEvent:FireServer()
end)
local function check(char)
for i, v in ipairs(char:GetChildren()) do
if v:IsA("Tool") and Tool == v then
return true
end
end
end
--first value in table is animation length and the second is the animation
local animationIndex = {
["1"] = swing1Animation,
["2"] = swing2Animation,
["3"] = swing3Animation,
["4"] = swing4Animation,
["5"] = swing5Animation
}
returnEvent.OnClientEvent:Connect(function(num)
if check(player.Character) then
slashSound:Play()
slashSound.PlaybackSpeed = math.random(100,150)/100
idleAnimation:Stop()
animationIndex[num]:Play()
hum.WalkSpeed = .5
firebrandHitbox:HitStart()
animationIndex[num].Stopped:Wait()
hum.WalkSpeed = 16
firebrandHitbox:HitStop()
if check(player.Character) then
idleAnimation:Play()
end
end
end)