Hey developers, I am attempting to make a part where if you are in a certain area of said part it would print('Welcome').
--local script
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local HRP = character:WaitForChild('HumanoidRootPart')
local detection = script.Parent.Parent.Detection
local mag = (detection.Position - HRP.Position).Magnitude
if mag <= 10 then
print('Welcome')
end
Hierarchy:
No errors in the output, it just doesn’t fire.
--server script (just an attempt)
local part = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local mag = (part.Position - char.HumanoidRootPart.Position).Magnitude
if mag <= 10 then
print('Welcome')
end
end)
end)
Honestly, I’m clueless, first time using magnitude,
any tips, suggestions or solutions would be greatly appreciated.
I think this is happening because you put this in a character added event. If I’m not mistaken, whatever is in a character added event will run when the character loads, then won’t run again. So, the character is spawning, the script sees that the character isn’t in the range of the part, and the if statement never runs again. Try using a while loop that will constantly check if the character’s magnitude from the part is lower than 10.
Once again, I’m not sure if this is the reason that your problem is happening, but please let me know if my idea works.
Thanks for reading this and I hope this helps!
Edit: your code can look like this in a local script:
while wait(0.5) do
if (game.players.localplayer.character:WaitForChild(“HumanoidRootPart”).Position - part.Position).Magnitude <= 10 then
print(“Yay, it works!”)
end
end
P.S I was kind of lazy writing this, so put some “WaitForChilds” in there
So I assume you’re trying to continually see whether a player is in range of the part or not? If so, here’s a solution.
Place this code in a script inside of the part:
-- Services
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- Variables
local part = script.Parent
RunService.Heartbeat:Connect()
for _, player in pairs(Players:GetPlayers()) do
playerPosition = player.Character.HumanoidRootPart.Position
if (part.Position - playerPosition).Magnitude <= 10 then
print("Welcome")
end
end
end
Here’s a breakdown. The code will:
Run the code every frame (which may be every 1/30th of a second, or higher such as 1/60th)
Loops through all the players in the game
Checks if each player is within a 10 stud distance of part
If they are, then it will print “Welcome”
If they are not, then it will continue to cycle through all the players
Another problem I found is that this is being called when a player joins.
So unless you want it to make sure a player is spawning in the correct location…
while true do --This will run the code every second.
wait(1) --Needed to stop studio from crashing lol.
local mag = (part.Position - pchar.HumanoidRootPart.Position).Magnitude --Executing that Var.
if mag <= 10 then --Checking distance.
print(‘Welcome’) --Doing whatever.
end
end
Doing this is very impractical though, and you should just use ontouch.
This is the way I would do it; with the Heartbeat event on the server.
Just be sure to properly connect the event to a function (You’ve left the :Connect() statement empty)
So that part would instead be:
RunService.Heartbeat:Connect(function()
for _, player in pairs(Players:GetPlayers()) do
playerPosition = player.Character.HumanoidRootPart.Position
if (part.Position - playerPosition).Magnitude <= 10 then
print("Welcome")
end
end
end)