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

help Unknown error? [Solved]

Asked by 8 years ago

i made the fallowing script, but i get a error involving an expected end, and i was hoping someone could help!

player = game.Players.LocalPlayer
mouse = player:GetMouse()


function FindNearestGrip()
local grips = game.Workspace:FindFirstChild("Grips", true):GetChildren()
table.sort(grips, function(a, b)
local dstA = (a.Position - player.Character.HumanoidRootPart.Position).magnitude
local dstB = (b.Position - player.Character.HumanoidRootPart.Position).magnitude
return a < b
end)
return grips[1]
player.Character.HumanoidRootPart.Position = grips[1]
end
end

mouse.KeyDown:connect(function(key)
if key == 'e' then
FindNearestGrip()
end)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Tab your code.

Between a function and an end or an if and an end is a block of code -- this code is "grouped" together.

To show how much of the code is controlled by a given function or an if, we increase the indentation between the start and end (between then and end).

Always tab your code.

If you tab the code you'd posted, you get this:

player = game.Players.LocalPlayer
mouse = player:GetMouse()


function FindNearestGrip()
    local grips = game.Workspace:FindFirstChild("Grips", true):GetChildren()
    local root = player.Character.HumanoidRootPart
    table.sort(grips, function(a, b)
        local dstA = (a.Position - root.Position).magnitude
        local dstB = (b.Position - root.Position).magnitude
        return a < b
    end)
    return grips[1]
    root.Position = grips[1]
end
end

mouse.KeyDown:connect(function(key)
    if key == 'e' then
        FindNearestGrip()

end)

This immediately reveals three problems:

  • You have code after a return
  • There is an incorrect second end at the end of FindNearestGrip
  • There is no end for the if key == 'e' then
Ad

Answer this question