My script, (2 scripts due to filtering enabled) makes it so when a model in the workspace (a union model named balloon) touches a coin, it gives the player whom the balloon belongs to (closest player I guess) money. My script for some reason, targets every player. Not the local one.
NOTE: By "At random times" I mean sometimes it targets the closest player, and sometimes it simply doesn't
This first script, is in the StarterGui with its only parent being the StarterGui itself.
local coincollect = workspace.Coins1:GetChildren() local player = game.Players.LocalPlayer for i, v in pairs(coincollect) do v.Touched:Connect(function(hit) if hit.Name == "Balloon" then game.ReplicatedStorage.BalloonWhenHit2:FireServer(player) end end) end
The second script is a script in ServerScriptService, with its only parent being the ServerScriptService itself.
local LP = script.LocalPlayerValue game.ReplicatedStorage.BalloonWhenHit2.OnServerEvent:Connect(function(player) LP.Value = player.Name local coincollect = workspace.Coins1:GetChildren() for i, v in pairs(coincollect) do v.Touched:Connect(function(hit) if hit.Name == "Balloon" then player.leaderstats.BB.Value = game.Players[LP.Value].leaderstats.BB.Value +5 v.Parent = game.ServerStorage wait(60) v.Parent = workspace.Coins1 end end) end end)
NOTE: Its not when the PLAYER touches the coin, its when the BALLOON does.
Thank you for reading this! And thank you more if you answer. if you need any more information before you answer, if you want to answer that is, don't hesitate to ask.
The reason that it seems like your script is targeting random players is actually quite calculated and precise.
The problem is thus:
First, RemoteEvents, like the one you're using, always slip in an extra argument that points to the player sending the request when the event is fired. So when you say "game.ReplicatedStorage.BalloonWhenHit2:FireServer(player)", the server is actually receiving two identical arguments.
Second, you're grabbing a table of all of the coins in the workspace when the player joins the game, not including the coins that have already been touched and placed into ServerStorage. This means that if the coins were not present when the player joined the game, it won't do anything when they're touched by a balloon.
Third, your connections are all over the place, and you make new ones every single time that a balloon is touched. It's extremely messy and makes it really hard to predict when the .Touched events will fire for whom.
You really don't need a LocalScript for this at all. It's actually just confusing things and making it harder for you. Here's an example of a server-side Script that can run from ServerScriptService all on it's own and do what you want:
(Just please note that I wrote all of this in one sitting and it might have errors. If so, please let me know and I'll help you fix it)
local coincollect = workspace.Coins1 local function findNearestPlayer(pos) local nearestpos = pos local nearest for idx, plr in pairs(game.Players:GetChildren()) do if (plr.Character.PrimaryPart.Position - pos).magnitude > (nearest - pos).magnitude then nearestpos = plr.Character.PrimaryPart.Position nearest = plr end end return nearest end for i, v in pairs(coincollect:GetChildren()) do v.Touched:Connect(function(hit) if hit.Name == "Balloon" then nearestPlayer = findNearestPlayer(v.Position) nearestPlayer.leaderstats.BB.Value = nearestPlayer.leaderstats.BB.Value + 5 v.Parent = game.ServerStorage wait(60) v.Parent = workspace.Coins1 end end) end