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

Why Walkspeed wont change when I press on the can?

Asked by 6 years ago

Hello, my question is why Walkspeed in Humanoid wont change when I press the can, so the script is :

function onClicked(part)
    h = part.Parent:FindFirstChild("Humanoid")
    if h ~= nil then
    h.WalkSpeed = 21
    script.Parent.Parent.Parent.REDBULL1:Destroy()
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

And the script is in LocalScript... So it wont work, please help !

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

If you take a look at the documentation for the MouseClicked event you will read the following.

Parameters:

playerWhoClicked

Type: Player

This indicates that when connecting to the event with a function, the first parameter will be the player that clicks the MouseDetector and not any part (think about, does a player click a MouseDetector with their head, torso, or leg?). That means we won't have to check for a humanoid like you do with the following.

    h = part.Parent:FindFirstChild("Humanoid")
    if h ~= nil then

Instead, humanoid will be defined relative to the player.

function onClicked(theplayer) -- I renamed the variable so it's more clear what it actually is.
    h = theplayer.Character:FindFirstChild("Humanoid")
    h.WalkSpeed = 21
    script.Parent.Parent.Parent.REDBULL1:Destroy()
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

P.S You MUST use a Script.

The MouseClick event must be handled by a Script.

Ad

Answer this question