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)
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)