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.
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.
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)