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

How to make onTouch check for something in the localplayer ?

Asked by
Bulvyte 388 Moderation Voter
7 years ago
Edited 7 years ago

How you do it ? I get error Workspace.Part.Script:5: attempt to index upvalue 'player' (a nil value)

local player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if player.PlayerGui:FindFirstChild("Test") then
            hit.Parent.Humanoid.Health = 0
        else
            print("Thing not found, yay")
        end
    end
end)
0
The error does not match this code? Where is 'upvalue' User#5423 17 — 7y

1 answer

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

You can always put a Script inside of the part and do like this

script.Parent.Touched:connect(function(hit)
    local player = game.Players:FindFirstChild(hit.Parent)

    if player ~= nil then
        if player.PlayerGui:FindFirstChild("Test") then
            hit.Parent.Humanoid.Health = 0
        else
            print("Thing not found, yay")
        end
    end
end)

A LocalScript will only run Lua code if it is a descendant of one of the following objects:
A Player's Backpack, such as a child of a Tool
A Player's Character model
A Player's PlayerGui
A Player's PlayerScripts
The ReplicatedFirst service
Note: the parent of the LocalScript will determine which client the Lua code will be executed on.

Also, LocalPlayer can only be referenced from the client, via LocalScript. Thus trying to call LocalPlayer anywhere on the server will not work.

More info: http://wiki.roblox.com/index.php?title=API:Class/LocalScript

0
Doesn't work. Bulvyte 388 — 7y
0
Is it a SCRIPT, not LocalScript inside of the part? TestingPlace1337 51 — 7y
0
I promise you that what I have above works. 100% just tested it. A script inside a part in workspace, with any item named 'Test' inside of PlayerGui. TestingPlace1337 51 — 7y
Ad

Answer this question