How do i make it so that 1 player colects the coin but the coin is stil there for other players

hello everyone
there are coins in my game and a sound if u collect them but if 1 person colects them they go away for other player and the sound also plays for everyone and i dont want that. can someone explain how i can fix that
collection 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 + 1
script.Sound:Play()
script.parent.Transparency = 1
wait(15)
db = true
script.parent.Transparency = 0
end
end
end)

You will have to do it locally, so a RemoteEvent might be helpful in this case. To also help clean the script a little and make it more efficient, check for the player in the beginning and use an if statement so your script doesn’t error.

A player always has a character, but a character sometimes doesn’t have a player. For example if an NPC touched that the whole script would error. You can also get rid of your humanoid check in that case, since there was a player found meaning a character did touch the coin. From there it is just using a remote event to mess with the coin properties.

Also use a capital C for connect, so you aren’t using the deprecated version.

i have never really worked with remote events could u maybe help me with, that this is just my first game

Why not make it so that in the local script it will detect if the humanoid touches a coin, and from there you can destroy it instead of having to utilize remote events.

RemoteEvents are basically just 1 way communication from the server-client/client-server

Think of it like this: You’re only limited of transporting things from either the server-side or the client side

RemoteEvents can be the solution for that! For this instance, we’d wanna put our RemoteEvent inside ReplicatedStorage so that it’s easy to replicate across both sides:

--Server Script that'll handle the part
local Part = script.Parent
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local AlreadyTouched = {}

Part.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player and not table.find(AlreadyTouched, Player.Name) then --This will check if we have a valid player & there's not a player who already touched it
        table.insert(AlreadyTouched, Player.Name)
        Player.leaderstats.Coins.Value += 1
        script.Sound:Play()
        Event:FireClient(Player) --Firing the Event
        wait(15)
        for Number, PlayerRemove in ipairs(AlreadyTouched) do
            if PlayerRemove == Player.Name then
               table.remove(AlreadyTouched, Number)
            end
        end

    end
end)
--LocalScript inside StarterPlayerScripts
local Player = game.Players.LocalPlayer
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Part = workspace:WaitForChild("Part") --Put this as where you want your coin to be

Event.OnClientEvent:Connect(function()
    Part.Transparency = 1
    wait(15)
    Part.Transparency = 0
end)

FireClient will fire a specific player using its argument to where we want to change the part properties too, & OnClientEvent will receive the event upon connecting it

(I’m assuming you’d want to have the Coins kept on the server side as well so I did a bit of configuration)

1 Like

He doesn’t want it destroyed though, he wants it invisible to the client-side for a couple of seconds then make it visible afterwards

Reference:

where do i need to put the first part i dont understand

What do you mean? I gave a couple of references to note where you can put the Part, but just put it inside the Part

does that mean i need to put it in the coin

If I understand it from your perspective yes, put it inside the Coin (Which is a Part if you didn’t know)

and what kind of script does it need to be

A server script- Mate have you even checked the Dev API or my comments at all yet

1 Like

You’d still have to use remote events to change the leaderstats anyway, so might as well be more secure and look for touched in the server.

1 Like