Struggling with "scope" of with function to set walkSpeed

Disclaimer - Been a long time since I did any RB coding!

I’m starting simple with a Sprint & Stamina bar feature for my character but am struggling with why my sprint function works only partially to set the walkSpeed.

local function sprint(shouldSprint)
	print("Sprint pressed", shouldSprint)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.WalkSpeed = 100
	
	if shouldSprint then
		print("Sprint!")
		--humanoid.walkSpeed = 300
		isSprinting = true
	else
		print("no sprint")
		--humanoid.walkSpeed = 16
		isSprinting = false
	end
end

The function above is called by the user Pressing keyboard key (for now). And fires as I expect. Using the code above sets the Players WalkSpeed. However, if I try and use the commented part in the logic, to toggle between setting the two values, I get an error:

 walkSpeed is not a valid member of Humanoid "Workspace.JayRey78.Humanoid"

What is happening here? Is there something I don’t understand about the “scope” of the variables inside the if?

2 Likes

WalkSpeed is the correct name to create path to from Humanoid, not walkSpeed

2 Likes

OMG. I literally just spotted that after typing all this out and then making an edit!
Thank you for the quick response!

I really wish the IDE would be more helpful in cases like this! Im spoiled with linting in VS Code!

2 Likes

Ya, Roblox IDE doesn’t have the ability to detect the properties of Humanoid since you are looking for it through WaitForChild. It simply thinks Humanoid as a string than an Instance. If it was an instance, the IDE will display the properties for you to select from. In other word, you either look up the property to see if it is spelled correctly or memorize it, which is easy to do if you use that property a lot.

1 Like