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

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

Asked by 4 years ago

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.

1--Assign Values
2 
3local Part = game.Workspace.PowerBox.Main
4local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
5local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")
6 
7if (Part.Position - HRP.Position).Magnitude < 8 then
8    print("working")
9end

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

01local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
02local UIS = game:GetService("UserInputService")
03local Part = game.Workspace.PowerBox.Main
04 
05UIS.InputBegan:Connect(function(keyCode)
06    if keyCode.keyCode == Enum.KeyCode.E then
07        if (Part.Position - HRP.Position).Magnitude < 8 then
08            print("working2")
09        end
10    end
11end)

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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:
01local Part = game.Workspace.PowerBox.Main
02local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
03local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")
04 
05while true do
06 
07wait()
08 
09    if (Part.Position - HRP.Position).Magnitude < 8 then
10 
11    print("working")
12 
13    end
14 
15end
  1. Heartbeat method:
01local Part = game.Workspace.PowerBox.Main
02local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
03local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")
04local UIS = game:GetService("RunService")
05 
06UIS.Heartbeat:Connect(function() --This uses the heartbeat event from runservice and runs the function every heartbeat (Frame)
07 
08    if (Part.Position - HRP.Position).Magnitude < 8 then
09 
10    print("working")
11 
12    end
13 
14end)

Hope this helped!

Ad
Log in to vote
0
Answered by
DemGame 271 Moderation Voter
4 years ago

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:

01--Assign Values
02 
03local Part = game.Workspace.PowerBox.Main
04local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
05local WriteToText = script.Parent:WaitForChild("ToolText"):WaitForChild("ToolText")
06 
07while wait() do
08    if (Part.Position - HRP.Position).Magnitude < 8 then
09        print("working")
10    end
11end

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.

Answer this question