How can I solve this Collectible Coin ERROR?

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)
1 Like

You shouldn’t check if the player has enough money in a local script, instead you should have that on the server.

Also are you doing the subtracting of the players money on the client?
If so you must do it on the server.

As @SpacialEthanRB mentioned. You shouldn’t check the player’s currency in client-side

I guess the problem is 1 You check player’s currency with client-side when client has currency more than 300 but server see it has 1 , So if client fire the server to decrease player’s currency again it’s will be back to default currency.

sorry for no-sense explain and if I miss something please tell me.

I didn’t understand anything. Where i am usibg the client side?

Should I change the local script toa script?

1 Like

The problem might be this line in the LocalScript. You’re getting a RemoteEvent simply called “RemoteEvent”, while the other currency related remotes have names, so you might be using the wrong RemoteEvent, correct me if I’m wrong.

No because previously I used a timer, I mean that avery 20 seconds a point was sum up and worked perfectly the shop, the currency and the remote event. But when I used the collectible Coin… everything stopped working

I don’t know if this is the issue, but it should be :FireServer, not :FireSever. Hope this fixes it!

CoinRE:FireServer(1) --I wrote 1 because is the amount its suppoused to be added

What’s the actual error? I can’t find any errors in this topic, and it’d help to know it.

OMG is true maybe thats the error, Im going to try it

Imagine a tool cost 300 points. You collect 300 points(from the collictable coins) and buy it. Everything goes as it should be but when you pick up another collectible coin(after buying the object) instead of changing from 0 points to 1 points it changes from 0 to 301 again

Ah, if it isn’t that typo, then I would check to save the coin data after buying the tool.

I’m talking about the error in the output, you said there was an error.

it didn’t worked the :FireServer error.
look what happend

Yes but I solved it and the error stills there

But can you show us the error in the output?

now there isn’t any error because lua thinks that everything is alright and is working as it should be but it isn’t

Im going to try to send everything to the server so that it can be modified the IntValue

Yeah, it looks like there’s an issue in your code that’s letting it turn into negative numbers, I have no idea how you’d solve that. Sorry. :confused:

I don’t know what the hell is happening but anything works