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

Workspace.Part.Script:2: attempt to index field 'LocalPlayer' (a nil value)?

local clickdetect = script.Parent:WaitForChild("ClickDetector")
local HungerVal = game.Players.LocalPlayer:WaitForChild("HungerVal")

clickdetect.MouseClick:Connect(function()
		print("test")
		HungerVal.Value = HungerVal.Value + 70
		script.Parent.Parent:Destroy()
  		if HungerVal.Value >100 then
   			HungerVal.Value = 100
	end
end)

help i dont know whats wrong

3 comments

0

why are you attempting to index the __local__player while this is a serverscript

0

cause i dont know how to use click detectors ;D

0

read the wiki then

1 answer

✓ Accepted answer

u cant index localplayer from a server script.

as for the clickdetectors,

if u did this:

local clickdetect = script.Parent:WaitForChild("ClickDetector")

clickdetect.MouseClick:Connect(function(plr)
       print(plr)
end)

then it would print the player which clicked it. Now that u got the player u can go and define the hungerval inside the thing;

local clickdetect = script.Parent:WaitForChild("ClickDetector")

clickdetect.MouseClick:Connect(function(plr)
        print(plr)
		local HungerVal = plr:FindFirstChild("HungerVal")

        HungerVal.Value = HungerVal.Value + 70
        script.Parent.Parent:Destroy()
        if HungerVal.Value >100 then
            HungerVal.Value = 100
    end
end)

or something

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