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

Trying to change a player's walkspeed via clickdetector?

I'm working on a game where I need a part that when the player looks at it, it reduces their walkspeed. I have next to no experience in LUA and what experience I do have is copy-pasting snippets of code into other code and crossing my fingers that it works. For this I've done a lot of research and work and I still can't figure it out. What I've been doing is taking the example code from the ROBLOX developer website:

script.Parent.ClickDetector.MouseHoverLeave:Connect(function(Player)
print("Oh my... " .. Player.Name .. " left the confines of my parent")
end)

And in place of line 2, I've just been copy-pasting vague "change the player's walkspeed" arguments and I don't understand. I know that there is some kind of "Player Who Hovered" argument, but there's been little in-depth documentation of click detectors that I can find, and I'm not experienced enough in LUA to get it. If you could help me, I would be a very happy boy.

For reference, a snippet of walkspeed code that I have been trying:

script.Parent.Touched:connect(function(p)
if p.Parent:FindFirstChild("Humanoid") then
	p.Parent.Humanoid.WalkSpeed = 40
	wait(10)
	p.Parent.Humanoid.WalkSpeed = 16

And of course, I've tried changing pieces of the code to work with click detector inputs, but this is my first day trying to do this kind of stuff.

1 answer

What we will do is take the event function from the example code

script.Parent.ClickDetector.MouseHoverLeave:Connect(function(Player)
	
end)

and add the code to change the walkspeed

p.Parent.Humanoid.WalkSpeed = 40
wait(10)
p.Parent.Humanoid.WalkSpeed = 16

But since I am getting Player with this parameter I need to get character

Like so

script.Parent.ClickDetector.MouseHoverLeave:Connect(function(Player)
	local char=Player.Character
	char.Humanoid.WalkSpeed = 40
	wait(10)
	char.Humanoid.WalkSpeed = 16
end)

We can even add a debounce so that the code won't run again if someone has hovered over it in less than 10 seconds

local db=true
script.Parent.ClickDetector.MouseHoverLeave:Connect(function(Player)
	local char=Player.Character
	if db==true then db=false
	char.Humanoid.WalkSpeed = 40
	wait(10)
	char.Humanoid.WalkSpeed = 16
	db=true
	end
end)

5 comments

0

The first Parameter is the Player object. Try:Player.Character:FindFirstChildOfClass("Humanoid") instead

0

Thank you, second person so much. I've been working on this for two hours. I love you. Thank you, so much.

0

@evil29 @Despayr OMG im so sorry, just realized I did that, updated the post

0

It's okay. Lol. Thank you both, I was going insane and now I am insanely pleased with this.

0

@evil29 np :D make sure to accept the answer if you found it helpful!

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