Hello everyone
Today I was experimenting with a collectible coin, that when you touch the coin, it disapear, a sound is played and an amount is sum to your leaderstats, in my case that amount is 1. It worked fine but I have also in my game some shops and when I gathered theamount of coins I needed for buying a tool, the amount that the object cost was subtracted and everything was fine. The problem is that when I went to pick up another collectible coin, the amount I had before buying the item came back and added 1 to the amount before, while what had to happen would be to add 1 to the amount after buy the item. To solve this error what I did was this:
- I created a remote event
- on the collectible coin script I wrote this:
local CoinRE = game.ReplicateStorage:WaitForChild("CoinRE") -- "CoinRE" is the name of the Remote event CoinRE:FireSever(1) --I wrote 1 because is the amount its suppoused to be added
- On the leadestats script I wrote the following lines:
local CoinRE = game.ReplicateStorage:WaitForChild("CoinRE") --The same variable as the Collectible Coin Script CoinRE.OnServerEvent:Connect(function(player, Value) player.leaderstats.Points.Value = player.leaderstats.Points.Value + Value -- Its suppoused that the . Value behind Points is the Value of the IntValue and the + Value is the (player, Value) one end)
After this the error kept coming out and I couldn’t think of any more ideas. Here I leave you all the materials that involve the error and if you need something else do not hesitate to write a comment. I hope you can fix the error and thanks for spending your time with my post. Greetings
Spideyalvaro999
MATERIALS
-The Collectible Coin Script
local coinPart = script.Parent local toggle = false local amount = 1 coinPart.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") ~= nil and not toggle then coinPart.Sound:Play() toggle = true local player = game.Players:GetPlayerFromCharacter(hit.Parent) local leaderboard = player:FindFirstChild("Leaderboard") local coins = leaderboard.Coins coins.Value = coins.Value + amount local coinClone = coinPart:Clone() coinPart:Destroy() wait(10) coinClone.Parent = workspace toggle = false end end)
- The Leaderboard Script
local replicatedStorage = game:GetService("ReplicatedStorage") local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("LeaderboardSave") local RemoteEvent = game.ReplicatedStorage.RemoteEvent game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder",player) leaderstats.Name = "leaderstats" local points = Instance.new("IntValue",leaderstats) points.Name = "Points" local HellCoins = Instance.new("IntValue", leaderstats) HellCoins.Name = "HellCoins" local HeavenCoins = Instance.new("IntValue", leaderstats) HeavenCoins.Name = "HeavenCoins" local AlienCoins = Instance.new("IntValue", leaderstats) AlienCoins.Name = "AlienCoins" local stats = ds:GetAsync(player.UserId) if stats ~= nil then points.Value = stats[1] HellCoins.Value = stats[2] HeavenCoins.Value = stats[3] AlienCoins.Value = stats[4] else points.Value = 0 HellCoins.Value = 0 HeavenCoins.Value = 0 AlienCoins.Value = 0 end end) game.Players.PlayerRemoving:Connect(function(player) local save = {} table.insert(save, player.leaderstats.HellCoins.Value) table.insert(save, player.leaderstats.HeavenCoins.Value) table.insert(save, player.leaderstats.AlienCoins.Value) table.insert(save, player.leaderstats.Points.Value) ds:SetAsync(player.UserId, save) end) RemoteEvent.OnServerEvent:Connect(function(player, Value, Item) -- player.leaderstats.Points.Value = player.leaderstats.Points.Value - Value game.ReplicatedStorage.Library[Item]:Clone().Parent = player:WaitForChild("Backpack") end)
- The Buy Button on the Shop(Is a local Script)
local player = game:GetService("Players").LocalPlayer local RemoteEvent = game.ReplicatedStorage.RemoteEvent script.Parent.MouseButton1Click:Connect(function(click) if player.leaderstats.Points.Value >= 300 then RemoteEvent:FireServer(300, "AlienTaser") end end)
- The Close button of the shop(I hinks is not a problem but I include it)
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("OpenAlienRE") RemoteEvent.OnClientEvent:Connect(function()--When the fired event is picked up this will run script.Parent.Parent.Visible = true end) script.Parent.Parent.CloseButton.MouseButton1Click:Connect(function() script.Parent.Parent.Visible = false end)