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