Im trying to make a system that each player haves a gun for 10 seconds, then going to the next player, the main problem is that when the gun is unequiped from the server, the idle animation doesnt stop, it keeps playing until reset
In the client i have these functions and events for unequip:
local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid: Humanoid = character:WaitForChild("Humanoid")
local animator: Animator = humanoid.Animator
local mouse = player:GetMouse()
local tool = script.Parent
local remote = replicatedStorage.Events.Shoot
local unequip = replicatedStorage.Events.UnequipTool
local animFolder = tool.Animations
local animations = {
Idle = animator:LoadAnimation(animFolder.Idle),
Shoot = animator:LoadAnimation(animFolder.Shoot)
}
local db = true
function stopAllAnimations()
for _,anim in pairs(animations) do
anim:Stop()
print("stopped: ", anim.Name, " Is playing: ", anim.IsPlaying)
end
print("stopped animations")
end
tool.Equipped:Connect(function()
animations.Idle:Play()
end)
tool.Activated:Connect(function()
if db then
db = false
animations.Shoot:Play()
remote:FireServer(mouse.Hit.Position)
task.wait(.5)
db = true
end
end)
tool.Unequipped:Connect(function()
stopAllAnimations()
print("Unequipped")
end)
tool.Destroying:Connect(function()
stopAllAnimations()
print("Destroying")
end)
And then on the server this:
function give_gun(plr: Player)
local backpack = plr:FindFirstChild("Backpack")
local humanoid = plr.Character and plr.Character:FindFirstChild("Humanoid")
if backpack and humanoid then
local gun = pistol:Clone()
gun.Parent = backpack
humanoid:EquipTool(gun)
end
end
function remove_gun(plr: Player)
local backpack = plr:FindFirstChild("Backpack")
local humanoid = plr.Character and plr.Character:FindFirstChild("Humanoid")
if humanoid then humanoid:UnequipTools() end
if backpack then backpack:ClearAllChildren() end
end
In my understaning, when unequiping or destroying, should fire the .unequip() or .destroying() events in the client, right?, i have that print message that executes when fire, well, doesnt show anything when the server removes gun.
You could destroy the animation and make it nil.
Or make it nil alone.
function stopAllAnimations()
for _, anim in pairs(animations) do
anim:Stop()
anim:Destroy() -- or you could use the Debris Service
anim = nil
print(anim)
end
print("Stopped animations!")
end)
I’ve done this with a tool in a game before, and it stopped. I hope it works for you!
You should always handle player animations on the client.
There is no “unreliability” with running animations on the client unless if you’re running animations on anything but your own character or something that isn’t local to your client.
(For example, a viewmodel is fine, playing animations on another player’s character isn’t)
Anyway, @Luke_gamer226, how much debugging have you done?
If you haven’t already done this, add more prints, see if things are firing correctly.
If nothing else works, you could always try handling the tool in a script parented to the character instead of the tool.
local toolConnections = {}
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local animator = character:WaitForChild("Humanoid"):WaitForChild("Animator")
local mouse = player:GetMouse()
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Shoot")
local lastFire = tick()
local animations = {
idle = animator:LoadAnimation(script.Idle),
shoot = animator:LoadAnimation(script.Shoot)
}
function childRemoved(child: Instance)
if not child:IsA("Tool") then return end
print("Tool Removed")
for _, connection in toolConnections do
connection:Disconnect()
end
for _, animation in animations do
animation:Stop()
end
print("Everything unbound and stopped")
end
--// there may be typos in below functions, I love writing code in browser
function childAdded(child: Instance)
if not child:IsA("Tool") then return end
print("Tool Added")
--// add name checks if needed
table.insert(toolConnections,
child.Activated:Connect(function()
print("Tool Activated")
if tick() - lastFire >= 0.5 then
lastFire = tick()
animations.shoot:Play()
remote:FireServer(mouse.Hit.Position)
end
end
)
table.insert(toolConnections,
child.Equipped:Connect(function()
print("Tool Equipped")
animations.idle:Play()
end
)
table.insert(toolConnections,
child.Unequipped:Connect(function()
print("Tool Unequipped")
animations.idle:Stop:()
animations.shoot:Stop:()
end
)
end
character.ChildAdded:Connect(childAdded)
character.ChildRemoved:Connect(childRemoved)
Only reason I mention this is that maybe the script is getting deleted before the code can execute, causing the animations to never stop. Its a little hard to debug though since you haven’t provided information on whether the prints that are there are printing or not.
So, i added print messages when executing stopAllAnimations(). when delete or unequip, that messages doesnt print, so i think the script is being deleted before can be executed, ill try sending an event from server before deleting the gun