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

Why does this print even if I am not 5 studs within the part?

So when I'm not even close it still prints this idk why

local mic = workspace.AnnouncerTable.MIC.Main
local range = 5

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(char)
		if char ~= nil then
			local torso = char.Torso
			if (mic.Position-torso.Position).magnitude >= range then
				print(p.Name.." Is close")
			end
		end
	end)
end)

4 comments

1

shouldn't it be <= range? Otherwise you're printing if the magnitude is higher than or equal to 5

0

sort of weird because when i do that and i change the range to above 100 then it works but eventhough im like 1000 studs away its weird honestly xd

1

Have you tried DistanceFromCharacter? Also wouldn't you need to fire a changed event or do an infinite loop to update the position?

0

ty

2 answers

Looking at your code, it only runs once when the player joins and when the player resets. If you take a look at line 8, you would see

if (mic.Position-torso.Position).magnitude >= range then

This basically says "if the mic part and the torso distance is bigger than or equal to the range variable then print. You would want to change it so it is if the distance is less than or equal. So just change the >= to <= and you should be set

I have tested this in-game and it will work, I noticed that you aren't looping the magnitude checking so I have put one in for you. As I have mentioned before, you needed to change >= to <=

local mic = workspace.AnnouncerTable.MIC.Main
local range = 5

game.Players.PlayerAdded:Connect(function(p)
    p.CharacterAdded:Connect(function(char)
		while wait() do
            local torso = char.Torso
            if (mic.Position-torso.Position).magnitude <= range then
                print(p.Name.." Is close")
            end
        end
    end)
end)

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