New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
0

# (Solved :D) My script targets every player at random times, but not the local one?

Asked by 7 months ago
Edited 7 months ago

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

1
Answered by 7 months ago

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.

SOLUTION

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)
v.Parent = game.ServerStorage
wait(60)
v.Parent = workspace.Coins1
end
end)
end

0
Thank you so much for answering the question, i'm surprised how complicated it actually was, and that I didn't need any remote events either! And thank you for not just pasting the script and saying "do this" because I actually was taught something as opposed to just being given a script and where to put it. Ozzisiya 68 — 7mo
0
The first error (and only so far) i've gotten says " 19:48:36.655 - ServerScriptService.BalloonWhenHit2:7: bad argument #1 to '?' (Vector3 expected, got nil)" the lines are line 7 and line 18. I know Vector3 has to do with positing objects. So maybe I can do it myself I doubt it though. Ozzisiya 68 — 7mo
0
I sadly still cannot figure this out. Ozzisiya 68 — 7mo
0
Ok, try changing "plr.Character.PrimaryPart.Position" to "plr.Character.Torso.Position" ChiefWildin 295 — 7mo
View all comments (4 more)
0
I'm not entirely sure why it's giving you that error. Those should all be Vector3 values. Also trying adding "print(plr.Character.Torso.Position)" after line 6 if it gives you the same error and tell me if it at least prints out a Vector3 value before it errors after that ChiefWildin 295 — 7mo
0
Before I get the error (By the way adding print(plr.Character.Torso.Position) didn't help) Anyway, Before I get the error "-2170.12671, 232.448669, -90.9307556" prints out in the output. That's the position of the LowerTorso (Due to R15) I am not very good with positioning but I think that's a Vector 3 Value. I trust you would know though. Ozzisiya 68 — 7mo
0
What about the nearest variable? Its set to nothing, nil. Ozzisiya 68 — 7mo
0
WOO HOO! With help from a moderator in discord, I've solved this. Ozzisiya 68 — 7mo