Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 5 years ago

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!

0
Also, your code is a mess, use indentation to help solve this, it makes it easier for us. AlphaGamer150 101 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
this help but I am not really asking for this, but I will still accept it! :D thx anyways! RainbowBeastYT 85 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

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! RainbowBeastYT 85 — 5y
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) yellp1 193 — 5y
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. yellp1 193 — 5y

Answer this question