How can I make a coin that disappears on the client and is still collectable by other players?

local db = true
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if db == true then
			db = false
			script.Parent.Transparency = 1
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
			script.Parent.Transparency = 1
			wait(30)
			db = true
			script.Parent.Transparency = 0
		end
	end	
end)

This script is for the whole server.

Player1 takes the coin and Player2 is not able to able to see or collect the coin until the 30 second cooldown is over.

I’m trying to make it so when Player1 collects the coin, Player1 cannot see or collect the coin again until the 30 second cooldown is over. Player2 can still see and collect the coin, even if Player1 is still having a cooldown. Each player has their own cooldowns until when they can next collect the coin.

How can I make the above script work locally?

3 Likes

I would change the debounce to instead of a boolean be a table that stores the player that picked the coin up and then checks everytime the function is fired if the player is on cooldown

local playerDebounce = {}
-- inside the event
if playerDebounce[hit.Parent.Name] == nil then
playerDebounce[hit.Parent.Name] = true

wait(30)
playerDebounce[hit.Parent.Name] = nil
end

This will make it so a player can’t collect it but others can for 30 seconds, however it is still visible for the player that is on cooldown, so for this I would fire a RemoteEvent with

RemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))

And then in the LocalScript reduce the transparency of the coin that has been collected, RemoteEvents can also pass arguments so you could add the coin that was collected to make it easier:

RemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent), script.Parent)
1 Like

Where in the original script should I add the first part of your solution? I don’t get any error messages but I’m pretty sure I’m not adding it to the proper place. Can you combine the original script and your addition?

Roblox Script instances now have a RunContext property that you can use to make Scripts that are children of Parts and Models in the Workspace run on the server or on any client. This means you can put a script on a Part that runs only locally, and hides it only for that player, and another script that is just the Server code. Script set to Client run context is basically a LocalScript that executes automatically in the workspace on all clients independently.

One caveat though, if you use Script.RunContext of Server or Client, rather than Legacy, you need to be aware that these scripts will currently run from within places like ReplicatedStorage or ServerStorage, without waiting to be added to the Workspace. If you have things you clone into the world from storage, you need to set any of these types of scripts to be disabled and manually enable them on the clones when they enter the workspace.

1 Like

Doesn’t work. I set the script to client and launched a 2 player test server. Player1 collects the coin and the coin disappears for 30 seconds for both Player1 and Player2. Player2 can’t collect the coin until the 30 second cooldown is up.

Replace the debounce you had “db” with the table debounce and the check

1 Like

Your script is not going to work exactly as is, you still need to check if touchingPlayer == Players.LocalPlayer.

The script runs on all clients, which means even Player2’s client is going to detect when Player1 touches the part and hide it locally.

For example:

local db = true
script.Parent.Touched:connect(function(hit)
	print("Touch event",hit.Parent)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player and player == game.Players.LocalPlayer then
			if db == true then
				db = false
				script.Parent.Transparency = 1
				wait(30)
				db = true
				script.Parent.Transparency = 0
			end
		end
	end	
end)
local playerDebounce = {}
script.Parent.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if playerDebounce[hit.Parent.Name] == nil then
			playerDebounce[hit.Parent.Name] = true
			wait(30)
			playerDebounce[hit.Parent.Name] = nil
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
		end
	end	
end)

Player hits the coin and gets the coin in 30 seconds. What do I do?

do

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1

before the wait(30)

1 Like

It worked! Thank you for your help!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.