Player Teleport Script skips Parts of Script when Activated while Walking

I have a script that blurs a players screen and forces them to stop moving when they click/touch a button then it teleports them, unblurs, and allows the player to move again, but it doesn’t unblur and allow the player to move again if they were moving as they clicked or touched the button.
No errors are shown.

This is a local script for a BillboardGUI button located in StarterGUI as I can only get it to work there.
(I attempted to fix the issue by placing parts of the script in an If statement, but it does nothing.)

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local lighting = game.Lighting

script.Parent.TextButton.MouseButton1Click:Connect(function()
	humanoid.JumpPower = 0
	humanoid.WalkSpeed = 0
	repeat wait()
		lighting.Blur.Size += 1
	until lighting.Blur.Size == 56

	if lighting.Blur.Size == 56 then
		character:MoveTo(game.Workspace.Locations.Playground.Location.Position)
		
		repeat wait()
			lighting.Blur.Size -= 1
		until lighting.Blur.Size == 6
		humanoid.JumpPower = 50
		humanoid.WalkSpeed = 16
	end
end)

The next is a script placed in a part that when touched, does the same as the last script.

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent.Character
	local humanoid = character.Humanoid
	local lighting = game.Lighting

	humanoid.JumpPower = 0
	humanoid.WalkSpeed = 0
	repeat wait()
		lighting.Blur.Size = lighting.Blur.Size + 0.5
	until lighting.Blur.Size == 56

	character:MoveTo(game.Workspace.Locations.Hills.Location.Position)

	repeat wait()
		lighting.Blur.Size = lighting.Blur.Size - 1
	until lighting.Blur.Size == 6
	humanoid.JumpPower = 50
	humanoid.WalkSpeed = 16
end)
1 Like

I see a few flags which I would like to ask if they work and if they do, how they work. But before I do that, please add debugging to see if your code and functions are executing properly. Add print() statements and print either string when a condition has been met or a point has been reached and also print out the variables. More about why in a bit.


BillboardGUI Scripts

The MoveTo() function is a humanoid function, which means it will use the humanoid and its current properties to execute said functions. With the walkspeed at 0, it will ‘move to’ the position with the speed of 0. If you insist on using the MoveTo() function, you have to disable the movement inputs and keep the walkspeed unchanged. The easier way is to just set the position of the HumanoidRootPart to that of the position you want, taking into account to add a Vector3.new(0, 2, 0) for the character hipheight.

character.HumanoidRootPart.Position = (game.Workspace.Locations.Hills.Location.Position + Vector3.new(0, 2, 0))

The if statement is excessive. the repeat statement will loop and discontinue the code from going any further than the loop iself. It will hold this up until the repeat conditions have been met, which is to be equals to 56. If you then check with another if statement after the loop if the blur is equals to 56 is unnecessary. It will always be 56 after the loop.


‘.Touched’ Scripts

The character will be the hit.Parent (e.g. character’s arm is a child of the player model). If you add a print statement to reveal this instance, I am pretty sure it will result in nil. This is why it’s important to add debugging prints if you’re stuck.


If it is still not working after my suggestions, please suggest other possibilities and show prints/debugging so I can help you further.

2 Likes

After some slight modifications, I’ve discovered that the first for loop continues to play even after it reaches 56. The second for loop DOES play, but it is just overun by the first for loop, leaving the blur at a switch between 56 and 57.

print(“3”) is the only statement that isn’t played.

I believe the issue lies in the first for loop, specifically in the until statement (Though, I’m not sure why it correlates to walking). It detects that it is no longer 56 when the second for loop plays, so it plays again until it is 56, putting it in a switch between 56 and 57.

Also, with wait() the first loop seemed to completely overtake the second loop (the second loop still plays), so I changed both to wait(0.1).

I also tried your MoveTo() idea, but it didn’t move the character body. It only moved the HumanoidRootPart, and as soon as I moved I was kinda just “flung out”.

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid
local lighting = game.Lighting

script.Parent.TextButton.MouseButton1Click:Connect(function()
	repeat wait(0.1)
		lighting.Blur.Size += 1
	until lighting.Blur.Size == 56

	character:MoveTo(game.Workspace.Locations.Hills.Location.Position)
	print("21")
	repeat wait(0.1)
		print("4")
		lighting.Blur.Size -= 1
	until lighting.Blur.Size == 6
	print("3")
end)

To clarify, you utilize repeat .. until loops, which work differently than for .. do loops. Repeat loops are most commonly used to halt code from running until a goal condition has been met, which makes the target of the loop the main focal point. With For loops, the goal is not for the target condition to be met, but it will know exactly how many times to be executed with the control value, end value and increment. Using this loop could be more beneficial for you, since it will execute a pre-set amount of times and afterwards, stop, whether you’re having issues with a float value like what is probably happening now.
(The blur value is for example 56.00003457346…, but since you only check for the exact digit 56, it is not the same number. You can fix this by making the condition >= 56.)

So examples of this are:

Using a repeat .. until loop

repeat 
	task.wait(0.1) --//Try to teach yourself to ALWAYS use task.[library]. Not using task is bad habit
	lighting.Blur.Size += 1
until lighting.Blur.Size >= 56 --//This takes float values into account

Using a `for … do loop

for blur = 0, 56, 1 do
--[[
	0 = Start value
	56 = End value
	1 = Increment value
	Now the amount of times it has to run has already been calculated,
	which is 56 / 1 - 0 = 56 times
]]
	lighting.Blur.Size += 1
end


I apologise if I was not clear enough in my previous post, but I suggested abandoning the idea and replacing it for the following;

When I mention the ‘position’, I speak of literal sense, and get the .Position of the HumanoidRootPart and set it to the position you want to be teleported to, adding 2 studs to the Y value to take into account for the hipheight of the character.
Example code which I gave in the previous post;

character.HumanoidRootPart.Position = (game.Workspace.Locations.Hills.Location.Position + Vector3.new(0, 2, 0))

So long story short, there’s multiple ways and loops to use to go about this issue, and (sometimes) I do not want to give out sections of code like a charity day because it’s much more fun and rewarding to be able to figure it out yourself with only hints or suggestions you’ve been given. To me, at least. I bet some people will debate me on that.


So again, I advise for you to figure it out yourself, but if not, click on the detail below for (what I think could be) the answer.

Suggested script

I’m not sure what the standard blur value is, but if it’s standard 6 then you use the 0, 50, 1, otherwise it’s 0, 56, 1 and etcetera.

script.Parent.TextButton.MouseButton1Click:Connect(function()
	for blur = 0, 56 do --//If you do not declare the increment, it will choose 1 as standard
		lighting.Blur.Size += 1
		task.wait(0.1)
	end

	print(lightning.Blur.Size) --//Debug, should print 56
	character:MoveTo(game.Workspace.Locations.Hills.Location.Position)
	task.wait(1)

	for unblur = 56, 6, -1 do
		lighting.Blur.Size -= 1
		task.wait(0.1)
		print(lightning.Blur.Size)
	end
end)
1 Like