For some reason, my script's magnitude doesn't work, "elseif" errors but I'm not sure why, and I don't know how to fix that, when I play is all it does is prints ("Found") and it doesn't check the new position for some reason, any help?
local part = game.Workspace.Part2 local billboard = game.Workspace.BillBoardPart.BillboardGui.TextLabel local cas = game:GetService("ContextActionService") local function onpress(action1) part.BrickColor = BrickColor.new("Black") end local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humRoot = character:WaitForChild("HumanoidRootPart") while (part.Position - humRoot.Position).Magnitude > 10 do wait(1) billboard.Visible = true print("found") cas:BindAction("action1", onpress, true, "t") end elseif (part.Position - humRoot.Position).Magnitude < 10 do wait(1) billboard.Visible = false print("didnt find") cas:UnbindAction("action1") end
Uhm, alright!
So, I don't know if this is going to help you at all, but I always do else if, so try doing that, maybe?
Hello!
So I noticed that there was an end before the elseif. But the elseif acts like an end that joins the two, so you'd put it in between both like this:
while (part.Position - humRoot.Position).Magnitude > 10 do wait(1) billboard.Visible = true print("found") cas:BindAction("action1", onpress, true, "t") elseif (part.Position - humRoot.Position).Magnitude < 10 do wait(1) billboard.Visible = false print("didnt find") cas:UnbindAction("action1") end
Note: I believe that you could just use two if statements, too. The elseif in this situation isn't completely necessary, but it does look cleaner.
Hope this helps! If this doesn't fix it, please comment and tell me whatever errors there are.
-ProqrammedGreen
while wait() do if (part.Position - humRoot.Position).Magnitude > 10 then wait(1) billboard.Visible = true print("found") cas:BindAction("action1", onpress, true, "t") elseif (part.Position - humRoot.Position).Magnitude < 10 then wait(1) billboard.Visible = false print("didnt find") cas:UnbindAction("action1") end end
I don't think you can do while (statement) do, elseif then. Because those are 2 different statements.
This should work as this will always check these parameters every 0.03 seconds.
Hope this helped!
well, you can't pair an elseif
with while
. elseif
can only be paired with an if
statement.
so here is your code modifed:
local part = game.Workspace.Part2 local billboard = game.Workspace.BillBoardPart.BillboardGui.TextLabel local cas = game:GetService("ContextActionService") local function onpress(action1) part.BrickColor = BrickColor.new("Black") end local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humRoot = character:WaitForChild("HumanoidRootPart") if(part.Position - humRoot.Position).Magnitude > 10 then wait(1) billboard.Visible = true print("found") cas:BindAction("action1", onpress, true, "t") elseif (part.Position - humRoot.Position).Magnitude < 10 do wait(1) billboard.Visible = false print("didnt find") cas:UnbindAction("action1") end
local part = workspace.Part2 -- use workspace instead of game.Workspace local billboard = workspace.BillBoardPart.BillboardGui.TextLabel local cas = game:GetService("ContextActionService") local function onpress(action1) part.BrickColor = BrickColor.new("Black") end local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humRoot = character:WaitForChild("HumanoidRootPart") if (part.Position - humRoot.Position).Magnitude > 10 then while (part.Position - humRoot.Position).Magnitude > 10 do wait(1) billboard.Visible = true print("found") cas:BindAction("action1", onpress, true, "t") end elseif (part.Position - humRoot.Position).Magnitude <= 10 do -- what if magnitude is exactly 10 wait(1) billboard.Visible = false print("didnt find") cas:UnbindAction("action1") end
I didn't test but u can't elseif a while do statement.