Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to change a player's walkspeed via clickdetector?

Asked by
evil29 0
5 years ago

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

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
The first Parameter is the Player object. Try:Player.Character:FindFirstChildOfClass("Humanoid") instead Despayr 505 — 5y
0
Thank you, second person so much. I've been working on this for two hours. I love you. Thank you, so much. evil29 0 — 5y
0
@evil29 @Despayr OMG im so sorry, just realized I did that, updated the post DanzLua 2879 — 5y
0
It's okay. Lol. Thank you both, I was going insane and now I am insanely pleased with this. evil29 0 — 5y
0
@evil29 np :D make sure to accept the answer if you found it helpful! DanzLua 2879 — 5y
Ad

Answer this question