I want my code to constantly check if the player is inside the hitbox area. However, it stops when it checks for magnitude > 10, and I can't figure out why.
01 | player = game.Players.LocalPlayer |
02 | repeat wait() until player:HasAppearanceLoaded() = = true |
03 | while true do |
04 | wait() |
05 | local magnitude = (player.Character.UpperTorso.Position - game.Workspace.GetOn.Position).magnitude |
06 | print (magnitude) |
07 | if magnitude < 9.9 then |
08 | if player and script.Parent.getOnScriptMoved.Value = = false then |
09 | getOnScript = game.ServerStorage.GetOnFrame:Clone() |
10 | getOnScript.Parent = player:WaitForChild( "PlayerGui" ) |
11 | wait() |
12 | script.Parent.getOnScriptMoved.Value = true |
13 | end |
14 | elseif magnitude > 10 then |
15 | guiLocation = player:WaitForChild( "PlayerGui" ) |
16 | if guiLocation.GetOnFrame then |
17 | guiLocation.GetOnFrame:Destroy() |
18 | end |
19 | end |
20 | end |
I figured out a way to recode this, and my problem has been resolved. I made two separate local scripts and put them inside StarterPlayer.StarterPlayerScrips, and messed with the code a bit. Here is what I put in each one to make it work.
01 | player = game.Players.LocalPlayer |
02 | repeat wait() until player:HasAppearanceLoaded() = = true |
03 | wait() |
04 | game.Workspace.GetOn.Touched:Connect( function () |
05 | local magnitude = (player.Character.UpperTorso.Position - game.Workspace.GetOn.Position).magnitude |
06 | if magnitude < 9.9 and game.Workspace.GetOn.Value.Value = = false then |
07 | wait() |
08 | if player and game.Workspace.GetOn.Value.Value = = false then |
09 | getOnScript = game.Workspace.GetOnFrame:Clone() |
10 | getOnScript.Parent = player:WaitForChild( "PlayerGui" ) |
11 | wait() |
12 | game.Workspace.GetOn.Value.Value = true |
13 | end |
14 | end |
15 | end ) |
01 | player = game.Players.LocalPlayer |
02 | repeat wait() until player:HasAppearanceLoaded() = = true |
03 | wait() |
04 | game.Workspace.Baseplate.Touched:Connect( function () |
05 | local magnitude = (player.Character.UpperTorso.Position - game.Workspace.GetOn.Position).magnitude |
06 | if magnitude > 9.9 and game.Workspace.GetOn.Value.Value = = true then |
07 | wait() |
08 | if player and game.Workspace.GetOn.Value.Value = = true then |
09 | getOnScriptParent = player:WaitForChild( "PlayerGui" ) |
10 | getOnScriptParent.GetOnFrame:Destroy() |
11 | game.Workspace.GetOn.Value.Value = false |
12 | end |
13 | end |
14 | end ) |