[FIXED] How do i make a coin collect only for a local player and not for the server?

Hello.

I wanted to make a coin giver who can give a certain amount of coins to a unic player.

My problem is that when i take a coin, an another player see the coin diseappear and hear the coin sound. And i don’t want this happend.

Do i have to put this script in a local script ??

local tt = true
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if tt == true then
			tt = 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)
3 Likes

What you can do is you can create the coin using a script, and then make the coin giving system in a LocalScript. Let me explain with a small code:

Server script:

local coins = Instance.new("Folder")
coins.Name = "Coins"
coins.Parent = workspace

local coin = YourCoinLocation:Clone()
coin.Parent = workspace

LocalScript:

local player = game:GetService("Players").LocalPlayer

local tt = true

for _,coin in pairs(workspace:WaitForChild("Coins"):GetChildren()) do
    coin.Touched:Connect(function(hit)
		if tt then
			tt = false
			coin.Transparency = 1
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
			script.Sound:Play()
			wait(1)
			script.Parent:Remove()
		end
    end)
end

What happens in a LocalScript is that the server doesn’t get involved in the code. This would make the coin disappear to the person who touched it, still remaining the same for another player.

Hope this helped.
-pranvexploder. :smiley:

1 Like

Okay, here’s what i done :

Local script :

local player = game:GetService("Players").LocalPlayer

local tt = true

for _,coin in pairs(workspace:WaitForChild("Coins"):GetChildren()) do
   coin.Touched:Connect(function(hit)
   	if tt then
   		tt = false
   		coin.Transparency = 1
   		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 25
   		script.Sound:Play()
   		wait(1)
   		script.Parent:Remove()
   	end
   end)
end

Server script :

-- Variables.

local datastore = game:GetService("DataStoreService")
local as1 = datastore:GetDataStore("CoinsSaveSystem")
local as2 = datastore:GetDataStore("ExpSaveSystem")

--	Function.

game.Players.PlayerAdded:Connect(function(plr)
	local folderr = Instance.new("Folder", plr)
	folderr.Name = "leaderstats"
	local coins = Instance.new("IntValue", folderr)
	coins.Name = "Coins"
	local exp = Instance.new("IntValue", folderr)
	exp.Name = "Experience"
	
-- Check the value saved or add the default new player amount.
	coins.Value = as1:GetAsync(plr.UserId) or 2500
	as1:SetAsync(plr.UserId, coins.Value)
	
	exp.Value = as2:GetAsync(plr.UserId) or 0
	as2:SetAsync(plr.UserId, exp.Value)
	
	coins.Changed:connect(function()
		as1:SetAsync(plr.UserId, coins.Value)
	end)
		
	exp.Changed:connect(function()
		as2:SetAsync(plr.UserId, exp.Value)
	end)		
	wait(1)
	print("Leaderboard : Okay !")	
end)

local coins = Instance.new("Folder")
	coins.Name = "Coins"
	coins.Parent = workspace

local coin = game.Players.LocalPlayer.leaderstats.coins:Clone()
	coin.Parent = workspace

The leaderboard is okay. But when i collect a coin nothing happened.

Remove is deprecated and has been for some time. Use Destroy instead.

1 Like

I see. You are using the same variable “coins” in two places.

Edit: Also, by the “YourCoinLocation” I did not mean the value in leaderstats. I meant the actual coin in workspace that would be visible to players and which could be collected.

1 Like

See my edit in the previous reply. Also, after seeing the screenshot, I will add to the edit: Move that coin behind you to ReplicatedStorage, in the place of “YourCoinLocation”, write:

game:GetService("ReplicatedStorage"):Coin

Could you send me the actual script instead of a screenshot? My net is down and it’s not able to render the image.
Also, remember that sending code as screenshot is a bad practice.

Sorry for that…

The “:coin:Clone()” appears in red


-- Variables.

local datastore = game:GetService("DataStoreService")
local as1 = datastore:GetDataStore("CoinsSaveSystem")
local as2 = datastore:GetDataStore("ExpSaveSystem")

--	Function.

game.Players.PlayerAdded:Connect(function(plr)
	local folderr = Instance.new("Folder", plr)
	folderr.Name = "leaderstats"
	local coins = Instance.new("Folder")
	coins.Name = "Coins"
	coins.Parent = workspace
	local exp = Instance.new("IntValue", folderr)
	exp.Name = "Experience"
	
-- Check the value saved or add the default new player amount.
	coins.Value = as1:GetAsync(plr.UserId) or 2500
	as1:SetAsync(plr.UserId, coins.Value)
	
	exp.Value = as2:GetAsync(plr.UserId) or 0
	as2:SetAsync(plr.UserId, exp.Value)
	
	coins.Changed:connect(function()
		as1:SetAsync(plr.UserId, coins.Value)
	end)
		
	exp.Changed:connect(function()
		as2:SetAsync(plr.UserId, exp.Value)
	end)		
	wait(1)
	print("Leaderboard : Okay !")	
end)

local coin = game:GetService("ReplicatedStorage"):Coin:Clone()
coin.Parent = workspace

It loaded, and that’s not how you fetch a child from a Service.
Replace the colon with a dot between

...orage").Coin....

When i run the game the leaderboard shows only the experience not the coins amount

Why have you set the “Coins” value to workspace? It needs to be in leaderstats or it won’t work.

Edit: I meant make a different folder for coins, not overwrite the coins value.

I don’t understand wich workspace do i have to chaange ?

-- Variables.

local datastore = game:GetService("DataStoreService")
local as1 = datastore:GetDataStore("CoinsSaveSystem")
local as2 = datastore:GetDataStore("ExpSaveSystem")

--	Function.

game.Players.PlayerAdded:Connect(function(plr)
	local folderr = Instance.new("Folder", plr)
	folderr.Name = "leaderstats"
	local coins = Instance.new("Folder", folderr)
	coins.Name = "Coins"
	coins.Parent = workspace
	local exp = Instance.new("IntValue", folderr)
	exp.Name = "Experience"
	
-- Check the value saved or add the default new player amount.
	coins.Value = as1:GetAsync(plr.UserId) or 2500
	as1:SetAsync(plr.UserId, coins.Value)
	
	exp.Value = as2:GetAsync(plr.UserId) or 0
	as2:SetAsync(plr.UserId, exp.Value)
	
	coins.Changed:connect(function()
		as1:SetAsync(plr.UserId, coins.Value)
	end)
		
	exp.Changed:connect(function()
		as2:SetAsync(plr.UserId, exp.Value)
	end)		
	wait(1)
	print("Leaderboard : Okay !")	
end)

local coin = game:GetService("ReplicatedStorage").Coin:Clone()
coin.Parent = workspace

I give up. Spoonfeeding time

-- Variables.

local datastore = game:GetService("DataStoreService")
local as1 = datastore:GetDataStore("CoinsSaveSystem")
local as2 = datastore:GetDataStore("ExpSaveSystem")

--	Function.

game.Players.PlayerAdded:Connect(function(plr)
	local folderr = Instance.new("Folder", plr)
	folderr.Name = "leaderstats"
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = folderr
	local exp = Instance.new("IntValue", folderr)
	exp.Name = "Experience"
	
-- Check the value saved or add the default new player amount.
	coins.Value = as1:GetAsync(plr.UserId) or 2500
	as1:SetAsync(plr.UserId, coins.Value)
	
	exp.Value = as2:GetAsync(plr.UserId) or 0
	as2:SetAsync(plr.UserId, exp.Value)
	
	coins.Changed:connect(function()
		as1:SetAsync(plr.UserId, coins.Value)
	end)
		
	exp.Changed:connect(function()
		as2:SetAsync(plr.UserId, exp.Value)
	end)		
	wait(1)
	print("Leaderboard : Okay !")	
end)

local coin = game:GetService("ReplicatedStorage"):Coin:Clone()
local coinsFolder = Instance.new("Folder")
coinsFolder.Name = "Coins"
coinsFolder.Parent = workspace
coin.Parent = coinsFolder

I don’t know if this is still the case, but awhile back when I tried doing .Touched events on the client via a LocalScript, it just wouldn’t fire at all. If this is still the case, you’d have to do the touched event on the server and then use a remoteEvent to fire the change to just the client who touched it.

It work’s but i can’t collect the coin. Is the problem the remote event ? like @DataErased said ?

script.Parent.RemoteEvent:FindFirstChild(function()

Its is that ?

Why don’t you just use the code if it works, in a local script placed in StarterPlayerScripts, as Local Scripts only work if they are a descendant of the Player, and index items separately like instead of script.Parent do

part = workspace:WaitForChild("Part")
   part.Touched:Connect(function(hit)) -- etc.

but then you will have to fire an event to transfer data for a player to the server, to add money etc. on the Server so it can store the value.
like below this you could do

event:FireServer(Amount)

Then on the Server “grab” that value like this :

event.OnServerEvent:Connect(function(player, Amount))
   player.leaderstats.Cash.Value = player.leaderstats.Cash.Value  + Amount

I find what’s wrong. Thank you all !