Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I compare the players location to all other players in the server?

So, I'm trying to find the difference between the player who sent the FireServer request, and all other players in the server to check if they are within 300 studs, I tried this but I recieved the error bad argument expected string got table.

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage.Remotes:WaitForChild("senseRemote")

remote.OnServerEvent:Connect(function(player)
	for i, v in pairs("game.Players:GetChildren()")
	do
	local inRange = (player.character.HumanoidRootPart.Position - 	v.Character.HumanoidRootPart.Position).magnitude
		if inRange < 300 and v.Team.Value ~= player.Team.Value then
			remote:Disconnect(player)
		end
	end
end)

3 comments

0

I will be adding more code after the in Range statement so changing the format completely won't work for what I need.

1

I think the player.Team doesn't need a ".value" after it ALSO remove the " between game.Players:GetChildren() (in the loop)

0

Thanks

1 answer

✓ Accepted answer

The "bad argument" error is because you're passing a string inside the pairs method.

local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage.Remotes:WaitForChild("senseRemote")

remote.OnServerEvent:Connect(function(player)
	for i, v in pairs(game.Players:GetChildren()) do
		local inRange = (player.character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).magnitude
		
		if inRange < 300 and v.Team ~= player.Team then
			remote:Disconnect(player)
		end
	end
end)

If you have any other concerns be sure to comment and we can figure it out :)

This discussion is preserved as it was. New answers, comments, and votes are closed.