What do you want to achieve? Keep it simple and clear!
I want to make a chat tags shop where you spend your coins for chat tags
What is the issue? Include screenshots / videos if possible!
The issue is that when i buy any item in the shop and then rejoin, the stats don’t save which makes me able to buy it again
Here is the script:
local Cost = 100
local ChatTagColor = "123,255,0"
local ChatTag = "Cool"
local plr = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if plr:WaitForChild("leaderstats").Coins.Value >= Cost and plr:WaitForChild("SecretFolder").ChatTag.Value ~= ChatTag then
plr.leaderstats.Coins.Value -= Cost
plr.SecretFolder.ChatTag.Value = ChatTag
plr.SecretFolder.ChatTagColor.Value = ChatTagColor
script.Parent.Text = "Equipped"
elseif plr:WaitForChild("leaderstats").Coins.Value < Cost then
script.Parent.Text = "Not enough coins!"
elseif plr:WaitForChild("SecretFolder").ChatTag.Value == ChatTag then
script.Parent.Text = "You already have it equipped!"
end
end)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I can’t find any solution.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local Wins = Instance.new("NumberValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats
local Coins = Instance.new("NumberValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
local SecretFolder = Instance.new("Folder",plr)
SecretFolder.Name = "SecretFolder"
local ChatTag = Instance.new("StringValue")
ChatTag.Name = "ChatTag"
ChatTag.Parent = SecretFolder
local ChatTagColor = Instance.new("StringValue")
ChatTagColor.Name = "ChatTagColor"
ChatTagColor.Parent = SecretFolder
local PlayerUserId = "Player_"..plr.UserId
local success, errormessage = pcall(function()
data = DataStore:GetAsync(PlayerUserId, data)
end)
if success then
Wins.Value = data.Wins
Coins.Value = data.Coins
ChatTag.Value = data.ChatTag
ChatTagColor.Value = data.ChatTagColor
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local PlayerUserId = "Player_"..plr.UserId
local data = {
Wins = plr.leaderstats.Wins.Value;
Coins = plr.leaderstats.Coins.Value;
ChatTag = plr.SecretFolder.ChatTag.Value;
ChatTagColor = plr.SecretFolder.ChatTagColor.Value;
}
local success, errormessage = pcall(function()
DataStore:SetAsync(PlayerUserId, data)
end)
if success then
print('Data Successfully Saved!')
end
if errormessage then
print('Data UnSuccessfully Saved!')
warn(errormessage)
end
end)
I copied this script from a tutorial almost a year ago the time when i didn’t have any database knowledge (now i do) but i don’t see to find the issue here
Oh then in that case you should be firing a remote to let the server know that the player is buying something and then do the coin subtraction on the server
So you would need to use an event to handle the currency change because if you save your data on the server then you would also need to save this on the server
You should be saving your data on the server anyways
I tried your suggestion and i got an error saying attempt to perform arithmetic (sub) on number and Instance
And here is the script
game.ReplicatedStorage.BuyEvent.OnServerEvent:Connect(function(plr,Cost,ChatTag,ChatTagColor)
plr.leaderstats.Coins.Value -= Cost --This line causes the error
plr.SecretFolder.ChatTag.Value = ChatTag
plr.SecretFolder.ChatTagColor.Value = ChatTagColor
end)
And this is where it fires the event
local Cost = 1125
local ChatTagColor = "255, 0, 243"
local ChatTag = "ChatTag"
local plr = game.Players.LocalPlayer
game.ReplicatedStorage.BuyEvent:FireServer(plr,Cost,ChatTag,ChatTagColor)
This is unnecessary as the player is automatically the first parameter when firing to the server
local event = game.ReplicatedStorage:WaitForChild('BuyEvent')
script.Parent.MouseButton1Click:Connect(function()
event:FireServer(Cost,ChatTag,ChatTagColor)
end)
local event = game.ReplicatedStorage:WaitForChild('BuyEvent')
event.OnServerEvent:Connect(function(player,Cost,ChatTag,ChatTagColor)
local stats = player:WaitForChild('leaderstats')
if stats.Coins.Value >= Cost then
player.stats.Coins.Value -= Cost
player.SecretFolder.ChatTag.Value = ChatTag
player.SecretFolder.ChatTagColor.Value = ChatTagColor
end
end)