[FIXED] How to make a coin disappear only for one player?

Hello i want to make a coin collect and the problem is when i collect a coin in-game the others players see the coin desappear and ear the collect sound. And I would like players to each have their in-game and collect sound pieces

Here’s my coin collect script :

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 + 25
			script.Sound:Play()
			wait(1)
			script.Parent:Remove()
		end
	end
end)

10 Likes

Use a local script inside of the StarterPlayer or StarterCharacter that will handle making the coin disappear and playing the sound, and you can just use this script that you have now to award the coins.

6 Likes

I’d make it local instead of Server sided.

Local meaning only that player will be effected by stuff. Also whatever belongs on the Client can be modified, give little to less trust to the client but of course don’t always make things server sided either.

Server means that whatever happens will be replicated and effect all players and effects the server.
For example deleting a part would replicate that deleting on ALL clients within that server.

But if you do that on the Client; It’s only effect THAT client and no one else.

For a coin to dispear and play a sound i’d set up a Remote Event.
Remote events can send info to clients and Server. But again whatever is sent from the client to server can be modified by cheaters so you would want to verify any info that is sent to the server.

You may want to just use a local script for the Coins and stuff.
As for giving currency the Remote Events can do that.

Also finding a player like that can cause errors so maybe do:

Part.Touched:Connect(function(Hit)
local plr = game.Players:GetPlayerFromCharacter(Hit.Parent) -- This finding the player from hit.
end)

When touched Send info to a Remote Event and server listens if that Remote Event is activated.

1 Like

Its doesn’t work :confused:

local db = true
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		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 + 25
				script.Sound:Play()
				wait(1)
				script.Parent:Remove()
			end
		end
	end)

Cfrt

Did you use a LocalScript and keep it in the part? If so, then that’s the problem. LocalScripts do not run in Workspace. Try putting it somewhere where it will run, like StarterPlayerScripts.

Actually, it would be much easier to leave it as a server script, and just fire a RemoteEvent for the client, and have the client play the sound, otherwise you’d have to check if the player that touched is the LocalPlayer.

local db = true
local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			if db == true then
				db = false
				plr.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
                remote:FireClient(plr, script.Parent)
                db = true --Make sure other players can get it
			end
		end
	end)

Client:

local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event

remote.OnClientEvent:Connect(function(part)
    part.ScriptName.Sound:Play() --Change script name to the name of the script
	wait(1)
	part:Remove()
end)
1 Like

All you have to do is put it in a local script, then it will run on the client side.

2 Likes

If these coins can be spent, or shown off, I would recommend handling the collection on the server and sending a message to the client via remote event for it to be deleted.

1 Like

local db = true
local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			if db == true then
				db = false
				plr.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
                remote:FireClient(plr, script.Parent)
                db = true --Make sure other players can get it
			end
		end
	end)


local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event

remote.OnClientEvent:Connect(function(part)
    part.Collect.Sound:Play() --Change script name to the name of the script
	wait(1)
	part:Remove()
end)

It doesn’t work, all i want to do is that when a player hit the coin, it gives him “25coins” and play a coin
sound. I put into my coin part a local script with the name of “Collect”. And for sure i want that the coins can be used in-game shops.
adwadawda.wmv (1,8 Mo)

This is actually easy to do. You could do this with filtering enabled if that’s the case. But if its a specific player then just do this:

local Whitelist = {"PlayerName"}
local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Part = game.Workspace.Part
   
if Player.Name == Whitelist[1] then do
    Part.Transparency = 1
    Part.CanCollide= false

      -- or just delete the part
end 
1 Like

And where do i have to put this script ? Is it a local script or is it a simple script, do i have to put this into workspace or in starter player or whatever.

it would be a game, not an admin system. you want all the players to be able to play.

2 Likes

I don’t understand anything of all this scripts. i’m gonna do an another game.

--in the part
local db=true
local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event
game.Players.PlayerAdded:Connect(function(p)
	Instance.new('Folder',p).Name='leaderstats'
	Instance.new('IntValue',p.leaderstats).Name='Coins'
end)
script.Parent.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr and db then
			db = false
			plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 25
            		remote:FireClient(plr, script.Parent)
            		db = true --Make sure other players can get it
		end
	end)


local remote = game.ReplicatedStorage.RemoteEvent --Make this the remote event
--In the local script in playerscripts.
remote=game.ReplicatedStorage.RemoteEvent
remote.OnClientEvent:Connect(function(part)
	workspace.SoundName:Play()
	part:Destroy()
end)
4 Likes

One way to do it is to use CollectionService.

Example:

You can use TagEditor to tag the coins with a tag called “Coin”

Then insert a local script to StarterPlayerScripts.
And do something like this:

local CollectionService = game:GetService("CollectionService")

local Debounce = true

for _,Coin in pairs(CollectionService:GetTagged("Coin")) do
	Coin.Touched:Connect(function(Hit)
		local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Player and Debounce == true then
			print("Script Here")
		end
	end)
end
2 Likes

Your solution is much better than mine, the only problem is that it doesn’t check if it is a player, but it should be fine.

I fixed it.

Note

30charlimitisagoodthing