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

Part.Position = humRoot.Position Magnitude Help?

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

4 comments

0

You can't do while and elseif, there has to be a starting if somewhere. As I know of.

0

Magnitude shouldn't be capitalized. (I don't know if this is true, but maybe give it a shot)

0

@Aeroporia Yes it can be capitalized, that's not the issue.

0

OHH

5 answers

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?

1 comment

0

Doesn't appear to work either, else if and elseif are both underlined in red.

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

1 comment

0

Doesn't appear to work either, Players.Auxatier.PlayerScripts.LocalScript:18: Expected 'end' (to close 'do' at line 13), got 'elseif'

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!

1 comment

0

Doesn't appear to work either, it just prints found every 1 sec, I'm not too sure

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

1 comment

0

I tried that also before, but then again, the bottom half of the script doesn't work when it gets to elseif. The Billboard still stays visible = true and it doesn't print anything below

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.

1 comment

0

Appears to not work either, just prints "found" not didn't find even after 10 studs away

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