Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What's the problem in the script?

I put this code in a LocalScript, and it'll run the script once the player touches the Brick with the script in it. Wheres my problem? I'm new to scripting...

local character = game.Players.LocalPlayer.Character

script.Parent.Touched:connect(function()
	if character.Humanoid.WalkSpeed == 16 then
		character.Humanoid.WalkSpeed = 20
	else
		character.Humanoid.WalkSpeed = 20
	end
end)

2 comments

0

Just noticed I don't need line 6 and 7 :/

0

Is there a reason you are using a LocalScript rather than a regular script?

1 answer

Accessing LocalPlayer is for when the script is running on that player's computer, inside things like GUIs and tools. In a server script, it doesn't mean anything in particular. Instead, you would need to get the character from the part which touched the object, like this:

script.Parent.Touched:connect(function(hit)
	local character=hit.Parent
	if character:FindFirstChild("Humanoid")then
		if character.Humanoid.WalkSpeed==16 then
			character.Humanoid.WalkSpeed=20
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.