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

I got a syntax error(I think) and I can't find. Can you tell what I did?

I got this error {Expected 'end' (to close 'function' at line 4), got 'else'}.


I'm sorry if this is a really simple and easy solution, but I don't know. I made a simple sprint and it worked and then I continued to work on it so that the player change the speed back to normal. But then I got this error and it stopped working.

This is my script in its entirety:


local var_x = 0
while true do
	wait(1)
	game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcesed)
    	if var_x == 0 then
		if input.KeyCode == Enum.KeyCode.X then 
        		game.Player.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 50
			local var_x = 1 
    		end 
	end
	else then
		if input.KeyCode == Enum.KeyCode.X then 
        		game.Player.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 17
			local var_x = 0 
    		end 
	end			
end

I know it's just a simple sprint button but I'm still confused.

1 answer

✓ Accepted answer
local var_x = 0
while true do
    wait(1)
    game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcesed)
        if var_x == 0 then
	        if input.KeyCode == Enum.KeyCode.X then 
	            game.Player.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 50
	            var_x = 1 
	        end 
	    else
	        if input.KeyCode == Enum.KeyCode.X then 
	            game.Player.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 17
	            var_x = 0 
	        end 
	    end  
	end)       
end

First of all, you do not add a then after an else statement secondly you do not add a local infront of a variable if you want to use it outside of its function and you dont add a end to an if statement if you want to add an else clause, you have to add the end after the else clause

1 comment

0

Thank you so much. You were a great help.

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