Script randomly stops working after Tween, only print() works?

So, I made a script inside a part that needs to give a player a speed boost, and adds some cool trail to both of the player’s feet. I use Tweening to make the part transparent after it has been triggered, but my script stops working after the “test” print, and I can’t figure out why.

Here’s the script:





script.Parent.Touched:Connect(function(hit)
	
	if game.Players:FindFirstChild(hit.Parent.Name) then
		
		local Info = TweenInfo.new(1)
		local Tween = game:GetService("TweenService"):Create(script.Parent,Info,{Transparency=1})

		
		local debounce = false
		
		local humanoid = hit.Parent:WaitForChild("Humanoid")
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local Boosted = player:WaitForChild("Boosted")
		
		if humanoid ~= nil and debounce == false and Boosted.Value == false then
			debounce = true
			script.Parent.BrickColor = BrickColor.new("Toothpaste")
			script.Parent.Material = "Neon"
			
			local att1 = Instance.new("Attachment", hit.Parent.LeftFoot)
			att1.Name = "trailatt"
			
			local att2 = Instance.new("Attachment", hit.Parent.RightUpperLeg)
			att2.Name = "trailatt"
			
			local trail1 = game.ReplicatedStorage.FlashTrail:Clone()
			trail1.Parent = hit.Parent.LeftFoot
			trail1.Attachment0 = att1
			trail1.Attachment1 = att2
			
			local att3 = Instance.new("Attachment", hit.Parent.RightFoot)
			att1.Name = "trailatt"

			local att4 = Instance.new("Attachment", hit.Parent.RightUpperLeg)
			att2.Name = "trailatt"

			local trail2 = game.ReplicatedStorage.FlashTrail:Clone()
			trail2.Parent = hit.Parent.RightFoot
			trail2.Attachment0 = att3
			trail2.Attachment1 = att4

			humanoid.WalkSpeed = 40
			Boosted.Value = true
			script.Parent.CanCollide = false 
			Tween:Play()
			print("tween")
			Tween.Completed:Wait()
			print("done")
			
			
			wait(1)
			
			print("test")
			wait(1)
			
			
			humanoid.WalkSpeed = 16	
			trail1:Destroy()
			trail2:Destroy()
			
			
		
		
		end
	end	
	
	
	
	
	
	
end)

You are not resetting the debounce.

That’s not the issue here. My script stops running after print("test") . It doesn’t matter if I reset the debounce there or not, the script won’t run those lines anyway.

Are you expecting for the stuff inside “if” run multiple times? Or do you want it to only run once and never again?

If former is the case, then you should reset the debounce, as Sleazel mentioned, and reset Boosted.Value too.

humanoid.WalkSpeed = 16	
trail1:Destroy()
trail2:Destroy()

debounce = false
Boosted.Value = false

i found out that u didnt put the Boosted.Value == false to true after it print test

That is not the issue here. The issue is that the script does not run the lines after the print("test). It doesn’t matter if I reset the debounce or Boosted value, because the lines won’t run anyways.

Edit: I will ofcourse add the debounce = false and Boosted.Value = false when i’ve fixed this issue.

So, you mean the WalkSpeed is not set back to 16? And the player can still run fast even after the 3 seconds pass?

Exactly! I have no idea why that is.

Could you print something after

humanoid.WalkSpeed = 16	
trail1:Destroy()
trail2:Destroy()

I’ve already tried that. Doesn’t print anything sadly.

Shouldn’t it be

local att3 = Instance.new("Attachment", hit.Parent.RightFoot)
att3.Name = "trailatt"

local att4 = Instance.new("Attachment", hit.Parent.RightUpperLeg)
att4.Name = "trailatt"

instead of

local att3 = Instance.new("Attachment", hit.Parent.RightFoot)
att1.Name = "trailatt"

local att4 = Instance.new("Attachment", hit.Parent.RightUpperLeg)
att2.Name = "trailatt"

?

Yes, that’s a small mistake i’ve made. This still doesn’t fix the problem though :frowning:

Your script had a bunch of errors thrown in the output, I fixed them, but it also turns out that the script probably doesn’t work as you intended it to, I fixed some of these problems, but I am not 100% sure what exactly you want the trails to do.

The first problem that didn’t throw an error was that you declared debounce in the wrong place, you need to declare it outside of the event handler’s scope, or else it will just always set it to false before you check whether it is false.

The second one that actually threw an error into the output was that trail1.Attachment0 returned nothing, because it hasn’t loaded yet, to make sure that the script doesn’t try to do anything to something that hasn’t loaded yet, you use :WaitForChild() function, so that the script does not continue until it verifies that this something has loaded.

So, you would write trail1:WaitForChild(“Attachment0”) = att1
instead of trail1.Attachment0 = att1

After I have fixed all of these, I get the same error you described in your original post. I will look into that and send you the fixed script after I find out what the problem was.

Edit: By the way, is there a specific reason why you need it to be

wait(1)
print("test")
wait(1)
--other code

but not

wait(2)
print("test")
---other code

?

It seems to work if you write it like in the second example.

Though I think I found the solution to keep these two waits and still make the code work, I will send it in a minute.

Edit2: It turned out my solution does not work, But you can still combine these two waits if you don’t need them separate.

Edit3: It seems like Tween.Completed:Wait() doesn’t run anything after the second wait after itself.

Edit4: Forget the WaitForChild thing I said about the attachments, You do that only if the attachments were objects inside the FlashTrail, which I thought they were, they actually turned out to be properties.

1 Like

I have copied your script to the studio and it works. I have moved debounce outside of function (it is useless now since new one is made each time function fires), but it worked even without changes. Therefore your problem lies somewhere else. Maybe some other script overwriting walkspeed or (however unlikely) rouge plugins. Baseplate.rbxl (23.2 KB)

Edit: It is also good idea to destroy attachments when you destroy the trails.

It doesn’t work for me, though. The WalkSpeed does not change back to normal and the trails are not destroyed.

Have you tried the file I provided? Does it work for you?

It does, though I don’t understand what change made it work, because when I tried their script, it didn’t work.

It may sound silly, but are you sure you are editing the right script? Maybe you have more speed pads and you are editing some other one?

1 Like

You can use code debug, set breakpoint at print("test") and use feature ‘Step Into’

It turned out I made some wrong assumptions. For example, I thought that FlashTrail was actually a part with attachments inside of it.

I fixed these things but the problem still persists.

The thing that confuses me the most, is that when I run your script in your file, it works, but when I copy it into my file, it doesn’t.

Edit: I found out that depending on whether the part that the player is supposed to touch, is anchored, the result will be different. If the part is anchored, then the code after the second wait after tween.Completed:Wait(), will work. If the part is not anchored, then it won’t.

This is the most confusing thing I have ever seen.