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

12:27:30.300 - Workspace.SellArea.SellHitbox.Script:4: attempt to index local 'plr' (a nil value)?

So, whenever the player touches the parent this error comes out. Script:4: attempt to index local 'plr' (a nil value)

script.Parent.Touched:Connect(function(hit)
local pService = game:GetService("Players")
local plr = pService:GetPlayerFromCharacter(hit.Parent)
plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + plr.templeaderstats.moneyCollected.Value
end)

In an older game I used a different system which worked but is way too complicated and old to use for a 2019 simulator.

3 comments

0

after plr put "if not plr then return end" or put if plr then ... end

0

@yHasteeD Still not working.

0

Posted a answer.

1 answer

✓ Accepted answer

For first, you can put the service of players before script.Parent.Touched

and the :GetPlayerFromCharacter(hit.Parent) only get the chracater if hit.Parent is the caracter if not will return nil, you can check if has a humanoid in player and if humanoid exists get character

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- Check if humanoid exists on touch, (you can use FindFirstChildOfClass("Humanoid") for check if the humanoid name has changed)
		-- Get character
	end
end)

after this only get player and if player is not nil.

Full script

local pService = game:GetService("Players")

script.Parent.Touched:Connect(function(hit) -- Detect hit
    if hit.Parent:FindFirstChild("Humanoid") then -- Check humanoid
	    local plr = pService:GetPlayerFromCharacter(hit.Parent) -- Get the player
	    if plr then -- Check if player is not nil
		    -- Add money
		    plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + plr.templeaderstats.moneyCollected.Value
	    end
    end
end)

6 comments

0

Looks like it's still not working. I have posted a gif here to show you: https://gyazo.com/3dfc151405283a0a3a038b225a77aa2d .

1

@OldBo5 you are changing the value in client, you need to change the value in server

0

@yHasteeD https://gyazo.com/2c46001758d1209edbbde375232a7339 I tried changing it in server in this GIF, but I still have 0 in the Money value.

0

you not changed in server

0

for change in server have a button named "Client" in right of Stop, only click it and will change to server mode

0

Now it works, I'm an idiot lol. Thanks! Accepted answer.

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