FarmerMax
FarmerMax

Reputation: 1

Roblox Studio- attempt to index nil with 'leaderstats' error in output

so i want to make this so when i click it gives me a "Coin" but it does not work & says attempt to index nil with 'leaderstats'

`game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats'

local coins = Instance.new('IntValue', leaderstats)
coins.Name = 'Coins'
coins.Value = 0
end)


game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function()
local currency = 'Coins'
local amount = 5
player.leaderstats[currency].Value = player.leaderstats[currency].Value + amount
end)``

Upvotes: 0

Views: 236

Answers (1)

kojocrash
kojocrash

Reputation: 341

You forgot the "player" parameter in OnServerEvent which is why it's nil

fix:

game.ReplicatedStorage.Remotes.Add.OnServerEvent:Connect(function(player) --added player parameter
local currency = 'Coins'
local amount = 5
player.leaderstats[currency].Value = player.leaderstats[currency].Value + amount
end)

Upvotes: 1

Related Questions