Issues with SpeedCoil, Custom Animations, Sounds and WalkSpeed

Hello! I’ve been working on one of my projects recently, this game uses custom player animations for walking and running (when you hold LeftShift.)
I got the idea to add a SpeedCoil tool in-game to help the player, however there are some issues i don’t know well how to fix.

First Problem: If the player presses the run button while having the SpeedCoil equipped it will trigger the running script, i tried to make it so everytime you equiped the coil it would disable the script but that didn’t work.

Second Problem: I want the SpeedCoil to have a custom running sound.
I tried to achieve this by Finding the HumanoidRootPart of the player when the speed was set, then Waiting for the “Running” Sound inside of it and changing the ID, but it still gave me no results.

Third Problem: I want the player to have a Custom Animation for moving (running in this case) when the tool is held, which i already have prepared but it’s just that i don’t know well how to implement it in from a script.

Is there any way to do this?

1 Like

Could you provide your scripts so we can better help you with your problems. As for the first problem, disabling the running script once the speed coil is equipped should work perfectly fine.

For the 2nd problem, I haven’t yet looked into changing the default walking sound, so I’ll look at some stuff and get back to you if I figure something out.

For the third problem, you would create a new Animation instance and input your AnimationId into it then you would load the animation and play it whenever the player is moving.
That would look something like this

local player  = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")

--make sure there is an Animator object parented to the humanoid so the animations will run
local Animator = humanoid:FindFirstChild("Animator")

local animation = Instance.new("Animation")
animation.AnimationId = "put the Id here"
local animationTrack = Animator:LoadAnimation(animation)

--now that your animation is loaded, you would play it whenever your character is moving
--you can check if your character is moving with Humanoid.MoveDirection(look up the documentation if you want more info on it)
if humanoid.MoveDirection.Magnitude > 0 then --here we check if the character is moving
	animationTrack:Play()
else
	animationTrack:Stop()
end

Let me know if you’re having any other issues and send over your scripts if possible.

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local CoilSound = Handle:WaitForChild('CoilSound')
local Character,Humanoid
local RunSpeed,SpeedConnection = 30,nil
local RunMain = Character:WaitForChild("RunMain")

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Humanoid = Character:WaitForChild('Humanoid')
	
	CoilSound:Play()
	Humanoid.WalkSpeed = RunSpeed
	if SpeedConnection == nil then
		SpeedConnection = Humanoid.Changed:Connect(function()
			if Humanoid.WalkSpeed ~= RunSpeed then
				Humanoid.WalkSpeed = RunSpeed
                RunMain.Enabled = false
				Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://6523472922"
			end
		end)
	end
end)
Tool.Unequipped:Connect(function()
	if SpeedConnection ~= nil then
		SpeedConnection:Disconnect()
		SpeedConnection = nil
	end
	Humanoid.WalkSpeed = 14
	Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://9083849830"
	RunMain.Enabled = true
end)

This is the script inside the default SpeedCoil Tool which i edited.
The Main Running Script is a LocalScript in StarterCharacterScripts.

RunMain.Enabled = false Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://6523472922"
Move these two lines of code further up the code after the WalkSpeed is set to RunSpeed for the first time. The reason the player can still sprint when the tool is equipped is because the sprint script is never disabled because this if statement – if Humanoid.WalkSpeed ~= RunSpeed then – stops the code under from being ran since Humanoid.WalkSpeed was already set to RunSpeed once the tool was equipped.

Let me know if there are any errors in the output

It stops in line 8, where it looks for the RunMain script
At first it output:

Attempt to index nil with ‘WaitForChild’

Then i tried changing it up only to be:

RunMain = Tool.Parent:WaitForChild("RunMain")

And it output 0 Errors, but it did state that the code ended in line 8 again, and the SpeedCoil wasn’t working.

RunMain returns nil because the character was not yet defined when you named the variable. It’s only defined inside the Tool.Equipped event

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local CoilSound = Handle:WaitForChild('CoilSound')
local Character = Tool.Parent
local Humanoid = Character:WaitForChild('Humanoid')
local RunSpeed,SpeedConnection = 30,nil
local RunMain = Character:WaitForChild("RunMain")

Tool.Equipped:Connect(function()
	CoilSound:Play()
	Humanoid.WalkSpeed = RunSpeed
	if SpeedConnection == nil then
		SpeedConnection = Humanoid.Changed:Connect(function()
			if Humanoid.WalkSpeed ~= RunSpeed then
				Humanoid.WalkSpeed = RunSpeed
                RunMain.Enabled = false
				Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://6523472922"
			end
		end)
	end
end)
Tool.Unequipped:Connect(function()
	if SpeedConnection ~= nil then
		SpeedConnection:Disconnect()
		SpeedConnection = nil
	end
	Humanoid.WalkSpeed = 14
	Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://9083849830"
	RunMain.Enabled = true
end)

It’s the same thing I just named the variables outside of the Tool.Equipped event

This now stops at Line 5, where it looks for the player’s Humanoid (also, this is in a server script, should it go in a localscript instead?)

Ignore what I just said normal scripts can be put in startercharacterscripts. Is the speedcoil script a local script?

i was also talking about the tool’s script not the running script- yes

Sorry I’m confused, so the script you sent me is it a local script or server script? The tool script you’re referring to is the script for the speedcoil, correct?

It’s the server script, which is placed inside the speedcoil (the tool).
The localscript is the running script i want to disable.

Got it. Try Character:FindFirstChild(“Humanoid”) instead and see if that works. If it doesn’t work send me your current script again so I can check for anything else

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local CoilSound = Handle:WaitForChild('CoilSound')
local Character = Tool.Parent
local Humanoid = Character:FindFirstChild('Humanoid')
local RunSpeed,SpeedConnection = 30,nil
local RunMain = Character:FindFirstChild("RunMain")

Tool.Equipped:Connect(function()
	CoilSound:Play()
	Humanoid.WalkSpeed = RunSpeed
	RunMain.Enabled = false
	Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://6523472922"
	if SpeedConnection == nil then
		SpeedConnection = Humanoid.Changed:Connect(function()
			if Humanoid.WalkSpeed ~= RunSpeed then
				Humanoid.WalkSpeed = RunSpeed
			end
		end)
	end
end)
Tool.Unequipped:Connect(function()
	if SpeedConnection ~= nil then
		SpeedConnection:Disconnect()
		SpeedConnection = nil
	end
	Humanoid.WalkSpeed = 14
	Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://9083849830"
	RunMain.Enabled = true
end)

The script now goes until line 11, where it sets the player’s speed, but when unequipped it also throws another output in line 27 where it’s UNequipped and the player’s speed is set back to normal. (Last time it didn’t do that.)

What does the output say and why does it stop at line 11?

Attempted to index nil with WalkSpeed

1 Like

Sorry, I just realized local Character = tool.Parent doesn’t work unless the tool is equipped. :disappointed_relieved:
There’s probably some other way to do this, but I would just change it to a local script so I can easily get the player using the tool that way.

local Tool = script.Parent
local Handle = Tool:WaitForChild('Handle')
local CoilSound = Handle:WaitForChild('CoilSound')
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild('Humanoid')
local RunSpeed,SpeedConnection = 30,nil
local RunMain = Character:FindFirstChild("RunMain")

Tool.Equipped:Connect(function()
	CoilSound:Play()
	Humanoid.WalkSpeed = RunSpeed
	RunMain.Enabled = false
	Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://6523472922"
	if SpeedConnection == nil then
		SpeedConnection = Humanoid.Changed:Connect(function()
			if Humanoid.WalkSpeed ~= RunSpeed then
				Humanoid.WalkSpeed = RunSpeed
			end
		end)
	end
end)
Tool.Unequipped:Connect(function()
	if SpeedConnection ~= nil then
		SpeedConnection:Disconnect()
		SpeedConnection = nil
	end
	Humanoid.WalkSpeed = 14
	Character:WaitForChild("HumanoidRootPart"):FindFirstChild("Running").SoundId = "rbxassetid://9083849830"
	RunMain.Enabled = true
end)

Hopefully there are no more errors I’m being kinda dumb right now

That does it now, thanks!
I still have one question though, where would the animation script go in this?

Are you playing the animation in the running script or in the tool script?

I want it to play in the Tool’s script with the Animation script you added in your first reply, but i don’t know where i could place it.

put the animation stuff inside tool.Equipped:Connect(function()
Also you won’t need to rename the player, character, and humanoid variables so you can just delete those