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

Part.Position = humRoot.Position Magnitude Help?

Asked by 4 years ago

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

0
You can't do while and elseif, there has to be a starting if somewhere. As I know of. killerbrenden 1537 — 4y
0
Magnitude shouldn't be capitalized. (I don't know if this is true, but maybe give it a shot) Aeroporia 37 — 4y
0
@Aeroporia Yes it can be capitalized, that's not the issue. CeramicTile 847 — 4y
0
OHH HappyTimIsHim 652 — 4y

5 answers

Log in to vote
0
Answered by 4 years ago

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?

0
Doesn't appear to work either, else if and elseif are both underlined in red. Auxatier 59 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Doesn't appear to work either, Players.Auxatier.PlayerScripts.LocalScript:18: Expected 'end' (to close 'do' at line 13), got 'elseif' Auxatier 59 — 4y
Log in to vote
0
Answered by 4 years ago
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!

0
Doesn't appear to work either, it just prints found every 1 sec, I'm not too sure Auxatier 59 — 4y
Log in to vote
0
Answered by 4 years ago

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

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 Auxatier 59 — 4y
Log in to vote
0
Answered by 4 years ago
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.

0
Appears to not work either, just prints "found" not didn't find even after 10 studs away Auxatier 59 — 4y

Answer this question