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

LocalPlayer is a nil value, how do I fix this?

Asked by 7 years ago
1--drgigabyte
2local soapWow = game.Players.LocalPlayer.Backpack:FindFirstChild("Soap")
3function onTouch(hit)
4if soapWow == true then
5soapWow = nil
6end
7end
8script.Parent.Touched:connect(onTouch)

1 answer

Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago

A normal script can't access LocalPlayer because it doesn't run on the client (aka you) and doesn't know who the LocalPlayer is. You can, however, get the person who touched the brick.

01local players = game:GetService("Players")
02function onTouch(hit)
03    if hit ~= nil and hit.Parent ~= nil then
04        -- Check to see if a player touched the brick
05        local player = players:GetPlayerFromCharacter(hit.Parent)
06        if player then
07            -- Make sure they aren't dead and they have soap
08            local hum = player.Character:findFirstChild("Humanoid")
09            local backpack = player:WaitForChild("Backpack")
10            local soap = backpack:findFirstChild("Soap")
11            if soap and hum and hum.Health > 0 then
12                soap:Destroy()
13            end
14        end
15    end
16end
17 
18script.Parent.Touched:connect(onTouch)
Ad

Answer this question