function onClicked()
local human = OnClicked.Parent:FindFirstChild("Humanoid")
if (human ~= nil) then
human.WalkSpeed = 50
end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)
Why does this speed giver w/ click detector not work?
1 answer
Hello.
First off,you never specified a variable to be returned by the MouseClick event. You need to specify a variable to be used by the event when it occurs.Also note that The MouseClick event returns the player who clicked it;not the Player's Character.
Here is some code that will change the player's walkspeed when the brick is clicked:
script.Parent.ClickDetector.MouseClick:connect(function(h)
if h.Character:FindFirstChild("Humanoid")~=nil then
h.Character.Humanoid.WalkSpeed=50
end
end)
I hope this clarifies!
If this is the answer you were looking for,don't forget to upvote it and mark it as the answer!
EDIT:
This code will also allow for a forcefield to be added.
script.Parent.ClickDetector.MouseClick:connect(function(h)
if h.Character:FindFirstChild("Humanoid")~=nil then
local f=Instance.new('ForceField',h.Character)
end
end)
8 comments
Apologies for the ">",I was trying to get a block quote,but it just wasn't working out.
I was wondering if you could do me a small favour. Could you possibly create a script for me where when you click a button you get a forcefield and it only works once?
You would do the same thing as above;get the character from the player who clicked it,add a forcefield,then disable the script.
Thanks.
No problem!
I'm having a problem. It is not working. Can you please post an additional answer, same format as before, with the forcefield?
Sure.
Edited my answer.
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Just a moment while I type my answer.
Okay.
There,I hope that answers your question! :)