Declare the variable outside because the other function doesn’t have the scope of the variable of the first function.
Example:
function foo()
local variable = 10 --'variable' is only exclusive to function 'foo()'
end
function printer()
print(variable) -- prints 'nil' because 'variable' is not a global variable
end
-- Edit: by removing the 'local' keyword, it just makes it a global variable, my bad
Here’s the code:
local animation
local dribbling
game.ReplicatedStorage.STORAGE.Remotes.DribbleBall.OnServerEvent:Connect(function(player)
local char = player.Character
dribbling = char:WaitForChild("Dribbling")
dribbling.Value = true
if dribbling.Value == true then
motor6d.Part0 = char.HumanoidRootPart
animation = char.Humanoid:LoadAnimation(animations.Dribble)
animation:Play()
ball.Parent = char
end
game.ReplicatedStorage.STORAGE.Remotes.DropBall.OnServerEvent:Connect(function(player)
dribbling.Value = false
animation.Looped = false
animation:Stop()
end)
end)
You shouldn’t nest connections within other connections.
local animation --so both functions have access to the variable
game.ReplicatedStorage.STORAGE.Remotes.DribbleBall.OnServerEvent:Connect(function(player)
local char = player.Character
local dribbling = char:WaitForChild("Dribbling")
if not dribbling then return end
if dribbling.Value then return end --make sure the ball isnt being dribbled
dribbling.Value = true
motor6d.Part0 = char.HumanoidRootPart
ball.Parent = char
animation = char.Humanoid:LoadAnimation(animations.Dribble)
animation:Play()
end)
game.ReplicatedStorage.STORAGE.Remotes.DropBall.OnServerEvent:Connect(function(player)
local char = player.Character
local dribbling = char:WaitForChild("Dribbling")
if not dribbling then return end
if not dribbling.Value then return end --make sure the ball is being dribbled
dribbling.Value = false
animation.Looped = false
animation:Stop()
end)
local AllPlayingAnims = {}
local function PlayAnim(player)
local char = player.Character
local dribbling = char:WaitForChild("Dribbling")
dribbling.Value = true
if dribbling.Value == true then
motor6d.Part0 = char.HumanoidRootPart
animation = char.Humanoid:LoadAnimation(animations.Dribble)
AllPlayingAnims[player] = animation
animation:Play()
ball.Parent = char
end
end
local function StopAnim(player)
if not AllPlayingAnims[player] then return end
dribbling.Value = false
AllPlayingAnims[player].Looped = false
AllPlayingAnims[player]:Stop()
AllPlayingAnims[player] = nil
end
game.ReplicatedStorage.STORAGE.Remotes.DribbleBall.OnServerEvent:Connect(PlayAnim)
game.ReplicatedStorage.STORAGE.Remotes.DropBall.OnServerEvent:Connect(StopAnim)
add a print statement in StopAnim function and confirms if it receiving any event on pressing Z and add a print statement in the if statement of Stop anim to check if its returning on the event.
local function StopAnim(player)
print("s")
if not AllPlayingAnims[player] then return end
player.Character:WaitForChild("Dribbling").Value = false
AllPlayingAnims[player].Looped = false
AllPlayingAnims[player]:Stop()
AllPlayingAnims[player] = nil
motor6d:Destroy()
end
.Looped is a little broken right now, and doesn’t replicate. This means it finishes on the server, but can loop on the client, so when you call :Stop, the server doesn’t send that to the client because it thinks the animation is completely finished, but the client thinks it’s still being looped and thus it keeps playing. If you switch to the server view, does it work correctly?