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

Why does this speed giver w/ click detector not work?

function onClicked()
local human = OnClicked.Parent:FindFirstChild("Humanoid") 
        
	if (human ~= nil) then
		human.WalkSpeed = 50
end
script.Parent.ClickDetector.MouseClick:connect(OnClicked)

3 comments

1

Just a moment while I type my answer.

0

Okay.

1

There,I hope that answers your question! :)

1 answer

✓ Accepted 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

1

Apologies for the ">",I was trying to get a block quote,but it just wasn't working out.

0

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?

1

You would do the same thing as above;get the character from the player who clicked it,add a forcefield,then disable the script.

0

Thanks.

1

No problem!

0

I'm having a problem. It is not working. Can you please post an additional answer, same format as before, with the forcefield?

1

Sure.

1

Edited my answer.

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