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

Magnitude only works with one player and not the rest?

Over here, a GUI should pop up only visible by you nobody else (that goes for everyone else) whenever you get close to another player in the game. The problem is, it loops through all the players and just picks one for some reason. How can I make it so it works for all players? Heres my code. Help would be greatly appreciated :)

Edit: It works fine with the print function, just shows the GUI for one of the players which is weird

while true do
	wait()
for i, v in pairs(game.Players:GetPlayers())do
	local magnitude = (player.Character.Head.Position - v.Character.Head.Position).magnitude
	if magnitude < 10 and v.Name ~= player.Name then
print("You are next to ")..v.Name
		billboard.Enabled = true
	else
		billboard.Enabled = false
		end
	end
end

If you can't figure it out I have an uncopylocked place that includes the same thing, if you have time, please look at it [https://www.roblox.com/games/914378398/Magnitude-Between-Players]

4 comments

1

Line 6; Didn't you mean "String" .. v.Name? :p

0

What do you mean "it just picks one for some reason"? Is it printing at all?

0

Thing is, it prints fine whenever I go next to any player in the game it says "you are next to(player)" but the billboard GUI only pops up for one of the players in the game which is strange

0

You can take a look at the uncopylocked place I made that has the same principle, test it with three players and you'll see the problem :(

2 answers

✓ Accepted answer

You're making the billboard gui unenabled even though it may have already been enabled by someone else. I'm asumming you do know that for i, v in pairs(game.Players:GetPlayers())do iterates through every player in no specific order. This means that with your current script, even if it finds a player close to you and enables the gui, the for loop will still keep going and the next player that 'v' becomes might not be close to the player, thus unenabling the gui even though there is a nearby player. This is why the gui only pops up for only one player; that player must the last player that the for loop checks for, thus it doesn't unenable the gui because it doesn't move on to find any player that is not nearby. You could easily fix this with an if statement that checks for a variable after the for loop that unenables the gui if the loop didn't find any close players like so:

while true do
	wait()
	local closeplayers = nil --Always reset to no players after the loop ends
	for i, v in pairs(game.Players:GetPlayers()) do
		local magnitude = (player.Character.Head.Position - v.Character.Head.Position).magnitude
		if magnitude < 10 and v.Name ~= player.Name then
			print("You are next to "..v.Name) --Also, this line was written wrong
			billboard.Enabled = true
			closeplayers = v.Name --If player found, change the variable
		end
	end
	if closeplayers == nil then --If there are no close players, set enabled to false
		billboard.Enabled = false
	end
end

Hope that helps

4 comments

0

Sort of, however the billboard still only enables for that one specific person in the game and nobody else. Maybe its because it keeps picking the same person but I was just up all night stressed trying to figure it out :/

0

I did the same thing I told you not to do, lol. I modified the script above. See if that works

0

It works! Thank you so much! I definitely learned something from this :)

0

Great to hear :)

Line 6; Didn't you mean "String" .. v.Name? :p

You also FORGOT TO DEFINE BILLBOARD!

Thank you, everyone, very much! Best answer here ever! See y'all later on and have a good night! This answer was brought to you by TheeDeathCaster! Thank you for readin! NOW STOP READIN.

2 comments

0

I swear if this actually gets upvotes I'm gonna lose it. XD

0

No I have billboard defined thats why the billboard GUI shows up but only for one of the players in the game and nobody else. And yes v.Name is a string but that doesn't matter cause it works.

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