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

I try looping this script fails to, I try the wile loop and the repeat loop,can anyone help?

I try looping this script fails to, can anyone help?

local Detector = script.Parent
local TimesClicked = 0
local target = game.ReplicatedStorage.ClickDetector
script.Parent.mouseClick:Connect(function(Player)
Detector:Destroy()
wait(5)
local ClickDetector = target:Clone()
ClickDetector.Parent = game.Workspace.Rock
TimesClicked = TimesClicked +0.5
if TimesClicked >= 3 and TimesClicked ~= nil then
Player.leaderstats.LifeFormFound.Value = Player.leaderstats.LifeFormFound.Value +1
script.Parent.Parent.Transparency = 1
script.Parent.Parent.CanCollide = false
wait(20)
script.Parent.Parent.Transparency = 0
script.Parent.Parent.CanCollide = true
	end
		end)

Someone help please!

1 comment

0

Also, your code is a mess, use indentation to help solve this, it makes it easier for us.

2 answers

✓ Accepted answer

On line 1, you set Detector equal to the parent of the script.

On line 4, you created an event witch will run when the Detector is used.

Line 5 is the first thing the script does when the Detector is used, this deletes the detector thus deleting the script (due to the fact the script is a child of the Detector), this will end script execution instantly.

Here is the solution:

local Detector = script.Parent
local TimesClicked = 0
local target = game.ReplicatedStorage.ClickDetector
script.Parent.mouseClick:Connect(function(Player)
local ClickDetector = target:Clone()
ClickDetector.Parent = game.Workspace.Rock
TimesClicked = TimesClicked +0.5
if TimesClicked >= 3 and TimesClicked ~= nil then
Player.leaderstats.LifeFormFound.Value = Player.leaderstats.LifeFormFound.Value +1
script.Parent.Parent.Transparency = 1
script.Parent.Parent.CanCollide = false
wait(20)
script.Parent.Parent.Transparency = 0
script.Parent.Parent.CanCollide = true
Detector:Destroy()
    end
        end)

Moving the Destroy() to the end of the script means that it happens last, this will give the script a chance to finish execution.

If this helped you, make sure to accept the answer, it will help us both out.

1 comment

0

this help but I am not really asking for this, but I will still accept it! :D thx anyways!

I see your problem. The script you're running is parented to Detector. When you call

Detector:Destroy()

in Line 05, it destroys the script, therefore stopping execution.

3 comments

0

First of all, the destroy is to destroy the script, second of all this can be a command because you are not giving the right answer or explanation!

0

Fine, I can do the same thing.. FIRST OF ALL, if you destroy a script, believe it or not, IT STOPS THE SCRIPT. Anything after the destroy doesn't get executed, Just as the man above you said, and therefore he did give the right answer. The answer was that the script wasn't executing and the explanation was that there was a section of code that destroyed the parent of the script (Continued)

0

thereby rendering everything else in the script useless. If you want to fix it then put the destroy after the rest of the code instead of before everything is ran and things should go smoothly. Also your code is very unorganized.

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