Making coins local

I’m making a coin script and need help making it local. Here is the thing though with my coins.
Whenever someone collects a coin, they cannot re-collect it forever. I’m also using DataStore 2. I have 2 scripts that control this, a local script in StarterPlayerScripts, and a script in ServerScriptService.

Here are the scripts:

ServerScriptService Script
local serverScriptService = game:GetService("ServerScriptService")
local dataStore2 = require(serverScriptService.DataStore2)


game.Players.PlayerAdded:Connect(function(player)
	
	local CoinsGathered = dataStore2("CoinsGathered", player);
	local coins = CoinsGathered:Get();
	
	if(coins == nil) then
		coins = {};
		CoinsGathered:Set(coins);
	end
	game.ReplicatedStorage.RemoteEvents.coinManagerEvent:FireClient(player,coins);
	
end);


game.ReplicatedStorage.RemoteEvents.coinGatherEvent.OnServerEvent:Connect(function(player, coinID)
	local coin;
	local coins = game.Workspace.Coins:GetChildren();
	
	for k,v in pairs(coins) do
		
		if(v.CoinID.Value == coinID) then
			coin = v;
			break;
		end
		
	end
	
	if(coin == nil) then
		print("Not a valid coin!");
	else
	
		--[[print(player:DistanceFromCharacter(coin.Position));]]--
		if(player:DistanceFromCharacter(coin.Position) < 35.0) then
			
			local CoinsGathered = dataStore2("CoinsGathered", player)
			local temp = CoinsGathered:Get();
			table.insert(temp, coinID);
			CoinsGathered:Set(temp);
			
			local coinStore = dataStore2("Coins", player);
			
			if(coinStore:Get() == nil) then
				coinStore:Set(0);
			end
			
			local lobbyCoins = dataStore2("LobbyCoins", player)

			lobbyCoins:Increment(1, player)
			coinStore:Increment(coin.CoinValue.Value, player)
		else
			print("Player isn't near the coin!");
		end
	end
end)
Local Script in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage");
 
local coinManagerEvent = ReplicatedStorage.RemoteEvents:WaitForChild("coinManagerEvent");
local coins = {};


-- This is triggered when the player loads into the game. This hides the coins already collected.

coinManagerEvent.OnClientEvent:Connect(function(coinList)
	
	local allCoins = game.Workspace.Coins:GetChildren()
	
	for k,v in pairs(coinList) do
		
		for k2,v2 in pairs(allCoins) do
			
			if(v == v2.CoinID.Value) then
				
				v2:Destroy();
				table.remove(allCoins, k2)
				
			end
		end
	end
	
	local TweenService = game:GetService("TweenService")
	
	local tweenInfo = TweenInfo.new(
		3, -- Time (25 is default)
		Enum.EasingStyle.Linear, -- EasingStyle
		Enum.EasingDirection.Out, -- EasingDirection
		-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
		false, -- Reverses (tween will reverse once reaching it's goal)
		0 -- DelayTime
	)
	
	local allCoins = game.Workspace.Coins:GetChildren()
	for k,v in pairs(allCoins) do
		
		local tween = TweenService:Create(v, tweenInfo, {CFrame = v.CFrame * CFrame.fromOrientation(0,0,math.rad(180))})
		tween:Play()
		local coinTouched = false
		
		v.Touched:Connect(function(model)
			 local player = game.Players:GetPlayerFromCharacter(model.Parent)
			 if player then
				if not coinTouched then
					coinTouched = true
					ReplicatedStorage.RemoteEvents.coinGatherEvent:FireServer(v.CoinID.Value)
					v:Destroy();
					
					table.remove(allCoins,k)
					game:GetService("SoundService").SoundEffects.Coin:Play()
					wait(1)
					coinTouched = false;
				 end
			 end
		end)
	end
end)



I am guessing the problem is in the local script, but I don’t know what it is. There are no errors in the output, and it adds coins to both players if one of them collects it (not what I want). Any help will do.

Have a great day! :smile:

You can use :FireClient() method to spawn the coins to the players that hasn’t collected any.

Save their data once they pick it up.
Just make sure the server checks if they actually picked up any before sending it over to the client.

Hello @MooskaisFlawless ,
I don’t recomend you make the script that controls the currency of your game local because you can suffer of explits.
Anyway if you can share your scripts will be great.

I did share my scripts, and the currency is controlled by the server.

for which script, and I already use it.

after lots of debugging, I found got the solution. This line here:

 local player = game.Players:GetPlayerFromCharacter(model.Parent)
 if player then

when really it should’ve been this:

 local player = game.Players:GetPlayerFromCharacter(model.Parent)
 if player == game.Players.LocalPlayer then
1 Like