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

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

Asked by 5 years ago
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

0
why are you attempting to index the __local__player while this is a serverscript lucldIy 3 — 5y
0
cause i dont know how to use click detectors ;D Cyrellejheff 11 — 5y
0
read the wiki then lucldIy 3 — 5y

1 answer

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

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

Ad

Answer this question