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.
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)