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

Why is my Magnitude script print not executing when I get near the selected object?

So I want a script to activate once i get near, I have a debug script that is not running when i test it, the script is located in StarterGui in a captions type GUI. When I get near the block, it does not print, here is some code snippets.

--Assign Values

local Part = game.Workspace.PowerBox.Main
local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")

if (Part.Position - HRP.Position).Magnitude < 8 then
	print("working")
end

However, in a simillar script, this works fine when I activate it.

local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Part = game.Workspace.PowerBox.Main

UIS.InputBegan:Connect(function(keyCode)
	if keyCode.keyCode == Enum.KeyCode.E then
		if (Part.Position - HRP.Position).Magnitude < 8 then
			print("working2")
		end
	end
end)

If anyone can help me figure out why it is not executing, please answer. If it is a problem, can I also have a fix to this? Many thanks.

-reidlab

2 answers

✓ Accepted answer

Edit: Someone answered this question while I was writing my answer! Your problem is that the script is not checking if you are within 8 magnitude after the 1st frame. The reason why the 2nd script works is that every time you have a keyboard input it connects the function. In that function is your magnitude check so every time you press E it checks your magnitude. A way to fix this is by adding a while true do loop. Although those kinds of loops are not the best for performance they do get the job done. A slightly better way to to check every frame is by using the heartbeat event. (This runs every single frame so will scale with the players framerate) There are most likely better ways to check the magnitude every frame but you will have to look for those on your own.

Here is a video showing you what to do if you managed to get confused

Here are the scripts if you want to just copy and paste them:

  1. While True Do loop:
local Part = game.Workspace.PowerBox.Main
local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")

while true do

wait()

	if (Part.Position - HRP.Position).Magnitude < 8 then
    
	print("working")
	
	end

end
  1. Heartbeat method:
local Part = game.Workspace.PowerBox.Main
local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")
local UIS = game:GetService("RunService")

UIS.Heartbeat:Connect(function() --This uses the heartbeat event from runservice and runs the function every heartbeat (Frame)

	if (Part.Position - HRP.Position).Magnitude < 8 then
    
	print("working")
	
	end

end)

Hope this helped!

Inside of your first script, the script only runs the "if" statement once and doesn't automatically loop unless you tell it to.

To loop it, I think the most efficient way would be to use a while() statement. Here is your edited code:

--Assign Values

local Part = game.Workspace.PowerBox.Main
local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")

while wait() do
	if (Part.Position - HRP.Position).Magnitude < 8 then
		print("working")
	end
end

Also, please indent your code by pressing the TAB key instead of pressing space 5 times. This would be very important in the future when you have longer code.

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