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
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
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.
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)
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!