How to make a coin local so that when one player collects it, the coin is still there for the other player

This category should only be used for open discussions on Roblox development!
Here is my Coin script:

Here is my leaderBoard script:

can someone tell me what im doing wrong

#help-and-feedback:game-design-support if you need help implementing specific game features.**

you should consider moving this thread to #help-and-feedback:scripting-support

how do i do that, im new to this

Go to the top of the post.

Click the Pencil.

Go down to the Category, it will say Development Discussion with a purple box.

Click it and scroll down till you see Help and Feedback Scripting Support and click it.

Then click Save or Update, you get the point. It should be at the bottom.

Hope this helps!

1 Like

Touched events are pretty iffy when done locally. What I would do -
Have a touched event, in a server script. When they touch, fire a remote event to the client (which deletes the coin on the client side), and add the money to the player’s inventory.

On the server side, don’t forget to store the fact that the player has picked up that coin - you can do this by inserting the player’s user ID in an array and referencing that next time they walk over the coin on the server side so you can make sure that players can’t pick up the same coin twice.

Good point. I didn’t actually cover the whole system, just gave the very basic how-to.

You can use a Remote to remove the coin on the client. Also this is in the wrong category put it in #help-and-feedback:scripting-support.

You can also just raycast downward out of the character’s torso and avoid firing a remote - unsure which is more efficient (or sensical depending on what needs to be tracked and known serverside).

1 Like

Yeah, it really just depends on what they are comfortable doing, along with how they are storing their information in their scripts. @dontjudgeme2026, if you have any other questions, feel free to Message me here.

everytime i use a remote event and add the script for the remote event into the PlayerScripts, for some reason the event does not fire and the coin is still deleted for both players

Alright, I think I have a solution. Assuming that you want a cooldown for each player I think this will work best. I tried to keep exploiters in mind and to not trust the client, so I will only send requests to the client with this one.

First, this is how I set up the system if you want to call it; in the explorer.


image

Now to the scripting part. If you are unfamiliar with some of the things in here, there are many YT videos that will explain it well. The API reference manual is a good place to look as well.
https://developer.roblox.com/en-us/api-reference

First, we will get started with the Client Script this is located inside the StarterGui and will make the coin invisible to you but not other players.

In the future, if you need help with scripting; you can use these " ``` " to make a code block.
Ex.

--code here

But onto the script.

local ReplicatedStorage = game:GetService("ReplicatedStorage");

local CoinTransparencyEvent = ReplicatedStorage:WaitForChild("CoinTransparencyModifier");

CoinTransparencyEvent.OnClientEvent:Connect(function(Object, CollisionSetting, CoinTransparencySetting)

Object.Transparency = CoinTransparencySetting;

Object.CanCollide = CollisionSetting;

end)

This script is short as it does not need to be used for much.

Now to the code that will be inside your coin.

local MemoryBank = {}; -- this is a table. you can store vales in here with it.
local CoinValue = 1; -- the amount that the coin is worth.
local WaitTime = 3; -- Seconds. The cooldown time for the coin.
local Coin = script.Parent; -- The coin
local ReplicatedStorage = game:GetService("ReplicatedStorage"); -- this gets the replicated storage
local Players = game:GetService("Players"); -- this gets the players
local CoinTransparencyEvent = ReplicatedStorage:WaitForChild("CoinTransparencyModifier"); --remote event. Passes infomation to client

local function OnTouch(Hit)
	print("Hit");
	if Hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(Hit.Parent);
		local CoinsValue = Player:WaitForChild("leaderstats"):WaitForChild("Coins");
		
		if not table.find(MemoryBank,Player) then
			warn("Added coins");
			CoinsValue.Value += CoinValue;
			table.insert(MemoryBank,Player);
			CoinTransparencyEvent:FireClient(Player,Coin, false, 1);
			
			wait(WaitTime);
			
			pcall(function()
				local Num = table.find(MemoryBank, Player);
				table.remove(MemoryBank,Num);
				CoinTransparencyEvent:FireClient(Player,Coin, true, 0);
			end)
		end
	end
end



Coin.Touched:Connect(OnTouch)

This is more complicated than your original code as it adds players to a table to see if the player has touched the coin.

I should explain the RemoteEvent now.

CoinTransparencyEvent:FireClient(Player,Coin, false, 1);

The player is the client we are wending the information over to. The coin is the coin we are changing the values of. false is use setting the CanCollide. 1 is setting the transparency.

I added this so you can change the properties of the coin on the client to your liking.

All you need to do now is add a remote event to ReplicatedStorage, name it CoinTransparencyModifier, and you should be done.

Hope this helps!

2 Likes

You’ll have to fire the remote from the server, in a touched event.

script.Parent.Touched:Connect(function(part)
remoteEvent:FireClient(game.Players:FindFirstChild(part.Parent.Name))
end)

Another tactic (maybe a better one) would be having all of your coins prespawned. Imagine you have all of your coins in in a folder, laying around the map (put them in a Folder named “Coins”). Name them 1, 2, 3, 4, 5, 6, etc. Then, have this code (read it):

--Server Script
local Remote = game.ReplicatedStorage.RemoteEvent -- your event here
local Remote2 = game.ReplicatedStorage.RemoteEvent2 -- your second event here

for i, v in pairs(game.Workspace:FindFirstChild("Coins"):GetChildren()) do
	v.Touched:Connect(function(part)
		if part.Parent:FindFirstChild("Humanoid") then
			Remote:FireClient(game.Players:FindFirstChild(part.Parent.Name),v.Name) -- You send the name of the part here.
		end
	end)
end
Remote2.OnServerEvent:Connect(function(sender,Amount)
	sender:FindFirstChild("leaderstats").Coins.Value = sender:FindFirstChild("leaderstats").Coins.Value + Amount -- you'll need to change what value is updating.
end)


--Local Script, in StarterGui
local Remote = game.ReplicatedStorage.RemoteEvent -- your event here
local Remote2 = game.ReplicatedStorage.RemoteEvent2 -- your second event here
local CoinAmount = 5 -- your coin amount
local Debounce = false
--First, set all of the coins to invisible.
for i, v in pairs(game.Workspace:FindFirstChild("Coins"):GetChildren()) do
	if v.Name ~= "1" then
		v.Transparency = 1
	end
end

--Now handle the remote
Remote.OnClientEvent:Connect(function(partName)
	if game.Workspace:FindFirstChild("Coins"):FindFirstChild(partName).Transparency ~= 1 then --make sure the client can see it then
		if Debounce == false then
			Debounce = true
			game.Workspace:FindFirstChild("Coins"):FindFirstChild(partName).Transparency = 1
			--Now make the next visible
			local Number = tonumber(partName) --May have gotten this part wrong
			game.Workspace:FindFirstChild("Coins"):FindFirstChild(Number).Transparency = 0
			Remote2:FireServer(CoinAmount)
			Debounce = false
		end
	end
end)

That would work. I didn’t test it, and you’ll want to improve the code/remote events, but it is a start.

thank you so much for responding. is the script in the coin different from the local script in the startergui or does the local script in the startergui control both collecting the coin and control the remote event?

Hey, so the longer script belongs in the coin. The shorter script belongs in StarterGui. The script in the StarterGui is different from the one in the Coin. Sorry if I didn’t make that clear.

thank you so much for you feedback and you were so helpful/ Once I test this and see how this goes, I will surely contact you back and let you know if this works and if i have any more questions, thank you for your kindness and patience.

1 Like

one more question. If I duplicate the coin will it mess up the coins ability to remain local for everyone or is it okay to copy it.

1 Like

It is ok to copy the coin. If we look at the scripts, the client (LocalScript) only changes information in the coin that is requested. Each touch is handled by the server and after the wait time, another request will be sent to the client to make the coin visible again.

in short, Yes.

the script works perfectly and I can copy them. I am so thankful for your help, you truly dont know how much you have helped me. I really appreciate it alot.

1 Like