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

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

Asked by
OldBo5 30
5 years ago

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.

0
after plr put "if not plr then return end" or put if plr then ... end yHasteeD 1819 — 5y
0
@yHasteeD Still not working. OldBo5 30 — 5y
0
Posted a answer. yHasteeD 1819 — 5y

1 answer

Log in to vote
1
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
Looks like it's still not working. I have posted a gif here to show you: https://gyazo.com/3dfc151405283a0a3a038b225a77aa2d . OldBo5 30 — 5y
1
@OldBo5 you are changing the value in client, you need to change the value in server yHasteeD 1819 — 5y
0
@yHasteeD https://gyazo.com/2c46001758d1209edbbde375232a7339 I tried changing it in server in this GIF, but I still have 0 in the Money value. OldBo5 30 — 5y
0
you not changed in server yHasteeD 1819 — 5y
View all comments (2 more)
0
for change in server have a button named "Client" in right of Stop, only click it and will change to server mode yHasteeD 1819 — 5y
0
Now it works, I'm an idiot lol. Thanks! Accepted answer. OldBo5 30 — 5y
Ad

Answer this question