If I Buy Something It Just changes My Cash To whatever it costs

Hey There Devs!!I Sorry If I Posted another help Post but when i buy a item in my tool shop it changes to the cost example:
the knife costs 75
and then when i buy it everything works perfectly but
it changes my cash to 75
my original cash is 100
here is the code

--variables--
local giveItemEvent = script.Parent.GiveItem
local folder = script.Parent.Parent.Folder --gets the folder

giveItemEvent.OnServerEvent:Connect(function(player,Leaderboard,item)
	Leaderboard.Coins.Value = Leaderboard.Coins.Value - folder.Cost.Value--minuses the coins
	item:Clone().Parent = player.Backpack--gives the tools
end)

Thanks For Reading!!
If Facing The Same Problem Will Be Sure To Tell You If Problem Solved!!
and i just noticed something in the folder its says it costs 75 but it minuses 25
and another thing when i bought it AGAIN it gave me 0 coins which i had 75 which is correct
but when i bought it again it now gave me NEGATIVE 25
:slight_smile:

2 Likes

i really dont know whats happening but just in case my leaderstats has the problem
then here’s the code

local data = game:GetService("DataStoreService"):GetOrderedDataStore("GetMoney00")
local function SaveData(plr,t)
	if plr and t then
		data:SetAsync(plr.UserId,t.Value)
	end
end
game.Players.PlayerAdded:Connect(function(plr)
	Instance.new("Folder",plr).Name = "Leaderboard"
	local stat
	local function Getleaderstats()
		wait(0.1)
		if not plr:FindFirstChild("Leaderboard") then
			wait(1.5)
			Getleaderstats()
		else
			stat = plr:WaitForChild("Leaderboard")
			return true;
		end
	end
	Getleaderstats()
	local function CreateStat(class,name,parent) 
		local i = Instance.new(class) 
		i.Name = tostring(name) 
		i.Parent = parent
		return i
	end
	local money = CreateStat("IntValue","Coins",stat)
	wait(0.03)
	local SavedLevel = data:GetAsync(plr.UserId)
	if SavedLevel then
		money.Value = SavedLevel
	else
		money.Value = 1
		wait(0.08)
		SaveData(plr,money)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	if plr:FindFirstChild("Leaderboard") and plr.Leaderboard:FindFirstChild("Coins") then
		SaveData(plr,plr.Leaderboard["Coins"])
	end
end)
if not game:GetService("RunService"):IsStudio() then
	game:BindToClose(function()
		for _,plr in pairs(game:GetService("Players")) do
			if plr:FindFirstChild("Leaderboard") and plr.Leaderboard:FindFirstChild("Coins") then
				SaveData(plr,plr.Leaderboard["Coins"])
			end
		end
		wait(10)
	end)
end

here is the explorer
Screenshot_3

1 Like

The game is private so there cant be exploiters

Leaderboard needs to be in lowercase (i.e. “leaderboard”) or else the leaderboard won’t pop up. And instead of writing that function inside of the player added event, you can simply set a variable to Instance.new():

local stat = Instance.new("Folder")
stat.Name = "leaderstat"
stat.Parent = plr --set it last for performance efficiency, trust me

Also, you have a lot of waits in your player added function, which means that while it’s in the middle of waiting, it’s not going to detect when another player joined the game.

From the looks of the explorer image, your remote event isn’t in Replicated Storage where both the client and the server can see it, it’s in the PlayerGui where only the client can see it.

You’re eventually going to make your game public, right? So, that means exploiters may come.

1 Like

You need to add some checks. If they fire the event, and say they have 25 cash, but the thing cost 50, the server will never check if they actually have enough and just put their cash negative.

local giveItemEvent = script.Parent.GiveItem
local folder = script.Parent.Parent.Folder --gets the folder

giveItemEvent.OnServerEvent:Connect(function(player,Leaderboard,item)
    if Leaderboard.Coins.Value >= folder.Cost.Value then --Checks if they have enough
	    Leaderboard.Coins.Value = Leaderboard.Coins.Value - folder.Cost.Value
	    item:Clone().Parent = player.Backpack--gives the tools
    end
end)
3 Likes

So i need to put a event checker? like i when they activate it it checks there leaderstats?

I Think The Starter Gear Is Bugged beacuse i have not bought a classic sword and it gave me duplicates of classic sword

I Think The Starter Gear Is Bugged beacuse i have not bought a classic sword and it gave me duplicates of classic sword

It isn’t required, but with data stores it’s always best to wrap it in pcall. And also, never trust the client. Don’t tell the server ‘I can get this item’ ask the server ‘Can I get this item?’. Hope this helps!

1 Like

Please don’t pass the leaderboard item from the client. An exploiter could easily just claim to have infinite coins with this system

1 Like