Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Leaderboard value not decreasing correctly?

Alright so I'll just oversimplifify this. I made a Leaderboard script and I placed it in ServerScriptService and then I made a Shop GUI but when I'm trying to purchase an item with the currency from my leaderboard, the "money" I have keep going down to -15 or -40 or -65 even if I have like say 99999999999 cash to buy the item, from my shop gui, it keeps going down to a negative number like -15 when purchasing that item.

the error I keep getting is : ServerScriptService.Leaderboard:24: attempt to call field 'UserId' (a number value)

So here's my Leaderboard script:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Currency")

game.Players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder",player)
	leaderboard.Name = "Leaderboard"

	local cash = Instance.new("IntValue",leaderboard)
	cash.Name = "Cash"
	cash.Value = 50
	
	local recieved,failed = pcall(function()
		data = dataStore:GetAsync("User-"..player.UserId)
	end)
	
	if recieved then
		cash.Value = data[1]
		print("Data was received")
	else
		print("Data was not received")	
	end
	
	while true do wait(30)
		dataStore:SetAsync("User-"..player.UserId{cash.Value})
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderboard = player.Leaderboard
	local cash = leaderboard.Cash
	
	local saved,failed = pcall(function()
		dataStore:SetAsync("User-"..player.UserId{cash.Value})
	end)
	
	if saved then
		print("Data was saved")
	else
		print("Data was not saved")
	end
end)

1 comment

0

ok can i get more answers

1 answer

✓ Accepted answer

Try this

dataStore:SetAsync(player.UserId, cash.Value)

It will work just the same. The "User-" isn't really required.

2 comments

0

It worked but now I'm getting this error: ServerScriptService.Leaderboard:17: attempt to index global 'data' (a number value)

0

pls I'll accept your answer

This discussion is preserved as it was. New answers, comments, and votes are closed.