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

Why is my touched script not working (how can I "reset" a script)?

In my game, there is a part which represents a seed on top of a part which is meant to look like soil. If a player steps on the soil part, the seed will slowly become transparent. After that, the function is disconnected. This part of the script is successful. However, after that part of the script, I want another part (sprout) to emerge after a player touches the soil again.

This is what I've tried and it's not working:

 --When the seed dissolves in the soil after being touched
seed = game.Workspace.Seed
   yop = script.Parent.Touched:connect(function(x)
	if x.Parent:FindFirstChild("Humanoid")then 
		yop:disconnect()
		for i= 0,1,0.01  do
		seed.Transparency = i
			
			
		
		
	wait()
	
	
	

	
		end		
end
end)

--a sprout emerges
script.Parent.Touched:connect(function(x)
if x.Parent:FindFirstChild("Humanoid")then
if game.Workspace.Seed.Transparency == 1 then
	for m = 1,0,-0.01 do 
		game.Workspace.Sprout.Transparency = m
	end
	wait()
end

3 answers

✓ Accepted answer

One thing that I found to be a problem in your script is the :disconnect() which is obsolete, or no longer of use in Roblox. It will end up erroring if you decide to print it. So to replace that to make sure they cannot touch it multiple times at once and screw up the script just add a debounce. This will make sure that your script can work when the player touches the part. Now, I assume you only want two things to happen once they touch it twice, not any loops back to the beginning so I would wright the code like this:

 --When the seed dissolves in the soil after being touched
-- Make sure this script is a descendant of the part 
 touch = true
seed = script.Parent
   seed.Touched:connect(function(x)
	print("Touch")

    if touch == true then
	if seed.Transparency == 1 then
		x.Parent:FindFirstChild("Humanoid")
		touch = false
		for i = 1,0,-0.1 do
			seed.Transparency = i
			wait(1)
			print("0")
			touch = true
		 
	end
	elseif seed.Transparency == 0 then
		if touch == true then
		x.Parent:FindFirstChild("Humanoid")
		touch = false
		for i = 0,1,0.1 do
			seed.Transparency = i
			wait(1)
			print("1")
			touch = true
		
		end
		end
	end
end
end)

If this helped don't forget to accept the answer and possibly give a thumbs up! If you have any questions feel free to comment on the answer and I will be glad to help. Thanks for asking the question!

7 comments

0

Ooh but when I do elseif, it does not work!

0

@crystalclay What is the error?

0

Elseif shows up underlined in red when I type it.

0

@crystalclay fixed it

0

This still did not work.

0

@crystalclay Ok now fixed it, i tested it and it works. But make sure this script is in the part.

0

Thank you for testing it! However, it is in the part, but it does not work and your script has a few mistakes. I noticed that you forgot to add "Sprout" into the part, and the part itself is supposed to be stepped on, not the seed . I'm sorry if I wasn't too descriptive

The best way to do this is to insert a "IntValue" into the player when they join, (which is game.Players.PlayerAdded) and that intvalue functions like this:

script.Parent.Touched:connect(function(x)
	if x.Parent:FindFirstChild("Humanoid")then
		if game.Workspace.Seed.Transparency == 1 then
		    if x.Parent:FindFirstChild(IntValueWeTalkedAbout) then
				if x.Parent:FindFirstChild(IntValueWeTalkedAbout).Value == 2 then
					for m = 1,0,-0.01 do 
				        game.Workspace.Sprout.Transparency = m
				    end
				    wait()
				end
			end
		end
	end
end)

PLEASE note that this is just pseudo code and is just set to be an example. Im just trying to give you some ideas. If you have any further questions, just reply!

script.Disabled = true wait() script.Disabled = false. Thats how you would 'reset' your script

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