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

help Unknown error? [Solved]

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

✓ Accepted answer

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

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