Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
1
shouldn't it be <= range? Otherwise you're printing if the magnitude is higher than or equal to 5 Vulkarin 581 — 5y
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 WillBe_Stoped 71 — 5y
1
Have you tried DistanceFromCharacter? Also wouldn't you need to fire a changed event or do an infinite loop to update the position? ABK2017 406 — 5y
0
ty WillBe_Stoped 71 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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

Ad
Log in to vote
0
Answered by
Vulkarin 581 Moderation Voter
5 years ago

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)

Answer this question