I want to make a coin script and every time I touch a coin it gives me currency/money. The problem is I have a shop and every time I buy the handgun after sometimes I can’t gain any money and the output says "ServerScriptService.CoinScript:15: attempt to index nil with ‘leaderstats’"I tried to look at my spawning script but there isn’t much I could find to why this happens.
Coin script/Spawning script:
local sp = workspace.SpawnParts:GetChildren()
local rs = game:GetService("ReplicatedStorage")
local cs = game:GetService("CollectionService")
local coin = rs.Coin
function makeCoin()
while task.wait(5) do
local randomnumber = math.random(1,#sp)
local chosenpart = sp[randomnumber]
local newcoin = coin:Clone()
newcoin.Parent = workspace.CoinFolder
newcoin.CFrame = chosenpart.CFrame
newcoin.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Money.Value = player.leaderstats.Money.Value + 5
newcoin:Destroy()
end)
end
end
spawn(makeCoin)
Shop script:
local rs = game:GetService("ReplicatedStorage")
local gun = rs.Handgun
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, text, cost)
print(text)
print(cost)
local money = player.leaderstats.Money
print(money.Value)
if money.Value >= cost then
local newgun = gun:Clone()
newgun.Parent = player.Backpack
money.Value = money.Value - cost
print("Something!")
text.Text = "Bought"
task.wait(1)
text.Text = "Buy Handgun"
elseif money.Value < cost then
print("nothing")
text.Text = "Insufficient Funds"
task.wait(1)
text.Text = "Buy Handgun"
end
end)
game.Workspace.TouchPart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.TouchEvent:FireClient(player)
end)
My datastorage script and leaderstats:
local DSS = game:GetService("DataStoreService")
local datastore = DSS:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
local folder = Instance.new("Folder", plr)
folder.Name = "leaderstats"
local money = Instance.new("IntValue", folder)
money.Name = "Money"
local data
local success, errormessage = pcall(function()
data = datastore:GetAsync(plr.UserId.."-money")
end)
if success then
money.Value = data
else
print("Error")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
datastore:SetAsync(plr.UserId.."-money", plr.leaderstats.Money.Value)
end)
if success then
print("Data Saving")
else
print("Error")
warn(errormessage)
end
end)
The local script I use in my gui to channel a lot of the functions hapenning:
local gui = script.Parent
local button = gui.Frame.TextButton
local exit = gui.Frame.ExitButton
local value = button.Value
local debounce = false
game.ReplicatedStorage.TouchEvent.OnClientEvent:Connect(function()
if not debounce then
debounce = true
gui.Enabled = true
exit.MouseButton1Click:Connect(function()
gui.Enabled = false
task.wait(3)
debounce = false
end)
end
end)
button.MouseButton1Click:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireServer(button, value.Value)
end)
I think the issue is most likely coming from the coin spawner script or the shop script since after I buy the handgun I struggle to collect coins after.