Ad
Log in to vote
0

How can I FIX INDEX NIL WHITH LEADERSTATS?

Asked by 1 month ago
Edited 1 month ago

it says in the output trying to index nil with leaderstats

Local TycoonNumber = 1

script.Parent.Touched:Connect(function(player) if player.Parent.Owner.Value == 1 then if game.Players.LocalPlayer.leaderstats.Money.Value <= 0 then game.ServerStorage.Buyable_Items.Droppers.Dropper1.Parent = game.Workspace.A end end end)

if it is impossable to fix then please tell

2 answers

Log in to vote
1
Answered by 1 month ago
Edited 1 month ago
Local TycoonNumber = 1

script.Parent.Touched:Connect(function(hit) -- this is not the player
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player.Parent.Owner.Value == 1 then 
        if game.Players.LocalPlayer.leaderstats.Money.Value <= 0 then
            game.ServerStorage.Buyable_Items.Droppers.Dropper1.Parent = game.Workspace.A 
        end 
    end 
end)
Ad
Log in to vote
0
Answered by 1 month ago

Since you are using the Touched function, which can only be used for server scripts, I'm assuming you're not using a local script. If so, in line 5, you are attempting to access the LocalPlayer, which you can only access from a local script. That's why it is returning nil. You can use game.Players:GetPlayerFromCharacter() on the parent of the "player" variable, then access the leaderstats from there.

local TycoonNumber = 1

script.Parent.Touched:Connect(function(player) 
    if player.Parent.Owner.Value == 1 then 
        local plr = game.Players:GetPlayerFromCharacter(player.Parent) -- gets the player from the character
        if plr.leaderstats.Money.Value <= 0 then -- NOW YOU CAN ACCESS THE LEADERSTATS
            game.ServerStorage.Buyable_Items.Droppers.Dropper1.Parent = game.Workspace.A 
        end 
    end 
end)

Hope it helps

Answer this question