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

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

--drgigabyte
local soapWow = game.Players.LocalPlayer.Backpack:FindFirstChild("Soap")
function onTouch(hit)
if soapWow == true then
soapWow = nil
end
end
script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

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.

local players = game:GetService("Players")
function onTouch(hit)
	if hit ~= nil and hit.Parent ~= nil then
		-- Check to see if a player touched the brick
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player then 
			-- Make sure they aren't dead and they have soap
			local hum = player.Character:findFirstChild("Humanoid") 
			local backpack = player:WaitForChild("Backpack")
			local soap = backpack:findFirstChild("Soap")
			if soap and hum and hum.Health > 0 then 
				soap:Destroy()
			end
		end
	end
end

script.Parent.Touched:connect(onTouch)

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