Switching animation not working

Hello, so I created two animations for a basketball game I created but when I attempt to have the user switch the side they are dribbling on (emulating a crossover) when they press “C” nothing happens without an error in the output. Any help is greatly appreciated.

Heres my script:

script.Parent.Anchored = false

local ballHandler = nil
local ball = script.Parent

local lefthandDribbleAnim = script.LeftHandDribble
local righthandDribbleAnim = script.RightHandDribble

local function attachBallToPlayer(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local motor6D = Instance.new("Motor6D")
	motor6D.Part0 = humanoidRootPart
	motor6D.Part1 = ball
	motor6D.C0 = CFrame.new(0, 0, 0) 
	motor6D.Parent = humanoidRootPart

	ball.Anchored = false
	ball.CanCollide = false
end

ball.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		ballHandler = player
		attachBallToPlayer(player)
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		local animator = humanoid:WaitForChild("Animator")
		local animationTrackLeftHandDribble = animator:LoadAnimation(lefthandDribbleAnim)
		local animationTrackRightHandDribble = animator:LoadAnimation(righthandDribbleAnim)
		animationTrackLeftHandDribble:Play()
		game:GetService("UserInputService").InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.C then
				animationTrackLeftHandDribble:Stop()
				task.wait()
				animationTrackRightHandDribble:Play()
			end
		end)
		
end
	end

Hey can you just do a print statement between each code after “if input.Keycode…” (other scripter will blame me for this debug but it works for me) just do print(“1”) just keep it simple and tell me what it prints :))

E.g.

-- E.G.
function TheErrorFunction()
    print("1")
    -- lines that do stuff
    print("2")
end
wait(0.1)
print("1")
script.Parent.Anchored = false
print("2")

local ballHandler = nil
local ball = script.Parent

local lefthandDribbleAnim = script.LeftHandDribble
local righthandDribbleAnim = script.RightHandDribble
print("3")

local function attachBallToPlayer(player)
	print("4")
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	print("5")
	local motor6D = Instance.new("Motor6D")
	motor6D.Part0 = humanoidRootPart
	motor6D.Part1 = ball
	motor6D.C0 = CFrame.new(0, 0, 0)
	motor6D.Parent = humanoidRootPart
	print("6")
	ball.Anchored = false
	ball.CanCollide = false
	print("7")
end

ball.Touched:Connect(function(hit)
	print("8")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		print("9")
		ballHandler = player
		attachBallToPlayer(player)
		local character = player.Character or player.CharacterAdded:Wait()
		local humanoid = character:WaitForChild("Humanoid")
		local animator = humanoid:WaitForChild("Animator")
		local animationTrackLeftHandDribble = animator:LoadAnimation(lefthandDribbleAnim)
		local animationTrackRightHandDribble = animator:LoadAnimation(righthandDribbleAnim)
		animationTrackLeftHandDribble:Play()
		print("10")
		game:GetService("UserInputService").InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.C then
				print("11")
				animationTrackLeftHandDribble:Stop()
				task.wait()
				animationTrackRightHandDribble:Play()
				print("12")
			end
		end)
	end
end)

Thanks for getting back! here’s a photo of the output:

no not all the lines just in the erroned function

so Tween:Stop() doesn’t really exist put Tween:Cancel() instead

and what you could add is

game:getService("UserInputService").InputBegan:Connect(function(input, GameProcessed)
  if GameProcessed then return end
  -- Your code
end)

even tho i dont think it will help the issues but its a lil add

also is this a client script ? i really recommend you to make the script somewhere else and use a remote to communicate between Client-Server :))

YOUR WELCOME (and its first time i got the “Solution” thingy ;)) im glad your my first one)

1 Like

yes! the issue was i needed server-client communication instead of just a server script from within the ball. thanks :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.