Collectible bugging out for Players

My problem is the following: I have collectible coins in my game and would like them to be obtainable by each person (obtainable locally). That way, the first person in my game to collect them won’t steal the coins from everyone else. I have already tried but I don’t understand enough to script it myself.

-- This normal script is placed in ServerScriptService and let's the players pick up the coins. (This is the only script that handles coins so this will need to change)

local Player = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")

for _, Coin in ipairs(CollectionService:GetTagged("CoinPickup")) do
	Coin.Touched:Connect(function(Object)
			if Object.Parent:FindFirstChild("Humanoid") then
				Coin:Destroy()	
		end  
	end)
end

The place in question (Test the game on a local server with atleast 2 players and collect a coin, that’s my problem that I want to solve):
CoinPickup Devforum.rbxl (43.3 KB)

You can use a remote event to delete it on the client and give the coins on the server.

You need to change the script, and put it under the character, and then use a RemoteEvent ad @SeasonedRiceFarmer said

So use a localscript to put under the character and communicate to the server via Remote events?

That’s exactly what you need to do, teh LocalScript in the character would destroy the coin (thus locally) and tell the Server to give the Player coins (make sure to do sanity checks!)

Put a remote event in ReplicatedStorage named Destroy.

--SERVER:

local Player = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")

for _, Coin in ipairs(CollectionService:GetTagged("CoinPickup")) do
	Coin.Touched:Connect(function(Object)
		if Player:GetPlayerFromCharacter(Object.Parent) then
			local plr = Player:GetPlayerFromCharacter(Object.Parent)
			plr.leaderstats.Coins += 69
			game:GetService("ReplicatedStorage").Destroy:FireClient(plr)
		end  
	end)
end

--CLIENT:
game:GetService("ReplicatedStorage").Destroy.OnClientEvent:Connect(function(obj)
   if obj then
      obj:Destroy()
   end
end)

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