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)
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:
return
end
at the end of FindNearestGrip
end
for the if key == 'e' then