Shop not working correctly

Hello, I’m trying to make a shop to purchase a sword. However, it’s not working right.

This is the leaderstats script:
function onPlayerEntered(newPlayer)
wait(.5)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”

local score = Instance.new("IntValue")

score.Name = "BrickCoins"
score.Value = 0

score.Parent = stats	
stats.Parent = newPlayer

end

game.Players.ChildAdded:connect(onPlayerEntered)

This is the Shop Script:
local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
if player.leaderstats.BrickCoins.Value >= 25 then
script.Parent.Text = “Purchased!”
script.Parent.Selectable = false
local Clone = game.ReplicatedStorage.ClassicSword:Clone()
Clone.Parent = player.BackPack
wait(2)
script.Parent.Text = “Sword (25 Brick Coins)”
script.Parent.Selectable = true
end
end)

And this is the Error message it’s giving me when I try to purchase it:
17:02:27.240 Players.gumixchan.PlayerGui.Shop.Frame.Prod1.Script:4: attempt to index nil with ‘leaderstats’ - Server - Script:4

1 Like

The problem is that the shop script is a server script, and not a local script. That means it runs on the server rather than on the player’s device. Since the server doesn’t have a player, this also means that the local player doesn’t exist so when you try to use game.Players.LocalPlayer, it will error.

To fix this, you need to make the shop script a local script.

1 Like