Why won't this work? (Scripting)

So I have a script

function onTouched(hit)
	local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
	local Controls = PlayerModule:GetControls()
	print("Disabled Movement")
	Controls:Disable()
	local human = hit.Parent:findFirstChild("Humanoid")
	if human then
		local anim = human:LoadAnimation(script.Parent.Animation)
		anim:Play()
		wait(1)
		Controls:Enable()
		print("Enabled Movement")
	end
end
script.Parent.Touched:connect(onTouched)

print("Working Script!")

I’m tryna make it so if I touch a part I stop moving and I do a animation then I be able to move again (Just like transfurmation, I just wanna make a test) But the script wont work. No errors.

Image: image

2 Likes

Capitalise the ‘f’ in findFirstChild

1 Like

I think you are putting too much effort into stopping movement, you can just anchor HumanoidRootPart to make the player unable to move or jump, that’s what I do at least.

No the Disable() function is fine.

1 Like
function onTouched(hit)
	local PlayerModule = require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
	local Controls = PlayerModule:GetControls()
	print("Disabled Movement")
	Controls:Disable()
	if  hit.Parent:FindFirstChild("Humanoid") then
        local human = hit.Parent.Humanoid
		local anim = human:LoadAnimation(script.Parent.Animation)
		anim:Play()
		wait(1)
		Controls:Enable()
		print("Enabled Movement")
	end
end
script.Parent.Touched:connect(onTouched)

print("Working Script!")

Try this

Is this a local script that you put in the Workspace?
In that case, local scripts do not run in Workspace.

You need to use RemoteEvents to communicate the server script placed in the Workspace to a localscript placed either StarterPacks/StarterPlayerScripts/StarterGui or so.

1 Like

And that yeah ofcourse, change it to a Server Script

if I do this, then the animation won’t play.

well if I make it a server script, then I couldnt make the player stop moving then able to move again.

Have you set the animations priority to “Action” in the animation editor? For me animations always played if i anchor humanoidrootpart.

I am currently working with a script that requires to play an animation while your character is stopped. I anchored the Humanoid’s RootPart and the animation works perfectly fine.

Try this and put the animation in the script and the script into the part.

local Animation = script.Animation --Replace Animation (script.Animation) with the name of the Animation object

script.Parent.Touched:Connect(function(touched)

if touched.Parent:FindFirstChild("Humanoid") then
      local AnimPlay = touched.Parent.Humanoid:LoadAnimation(Animation)
      touched.Parent.HumanoidRootPart.Anchored = true
      AnimPlay:Play()
      AnimPlay.Stopped:wait() --Waits until it finished playing
      touched.Parent.HumanoidRootPart.Anchored = false
end
end)

I have good news and some bad news.

The good news is I managed to fix it!! :smiley:
the help of @nLocaI and @slothfulGuy!

The bad news is when I touch the part and do the animation it repeats the animation because I’m still on it touching it. how can I make it so if I touch it and I try touching it again it wont work?

Disconnect the touched connection, so it won’t register any further events.