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

Problem with script that worked correctly but only once time then it got break?

Im OriginalDevelops, Im scripting a machine that player clicked and then it spawn a npc, then after a seconds the npc will automatic dead and be removed from the workspace, then the machine will be automatic refresh! This is my script ( normal script not local ) :

local ClickDetector = script.Parent.Detector
local part = script.Parent
local debounce = false

local NPCSpawn = script.Parent.Parent.SpawnPart
local NPC = game.ServerStorage.NPCs.Noob
local newMachine = game.ServerStorage.SpawnMachine

ClickDetector.MouseClick:Connect(function()
	if not debounce then
		debounce = true
		local cloneNoob = NPC:Clone()
		cloneNoob.Name = "Noob"
		cloneNoob.Parent = NPCSpawn
		part.SurfaceGui.TextLabel.Text = "Cooldown..."
		part.Color = Color3.fromRGB(255,0,0)
		wait(30)
		part.SurfaceGui.TextLabel.Text = "Spawn"
		part.Color = Color3.fromRGB(0,255,0)
		cloneNoob.Humanoid.Health = 0
		wait(10)
		cloneNoob:Destroy()
		print("Cooldown Completed!")
		local cloneMachine = newMachine:Clone()
		cloneMachine.Name = "SpawnMachine"
		cloneMachine.Parent = workspace
		print("New Machine Has Been Spawned!")
		wait(0.001)
		part.Parent:Destroy()
		print("Old Machine Has Been Destroyed!")
	end
end)

The issue is, all the machine is still working good but the machine can only be refresh once then the machine automatic disappered! The upper code still run, the npc still automatic get destroyed but the machine didnt get clone! That mean the upper code work infinity but the down code only worked once! Can anyone help me with this?

2 comments

0

Do you ever set debounce to false? If not try setting debounce to false after line 30.

0

When I set that, the noob not being destroy or kill and the machine being cooldown forever

1 answer

✓ Accepted answer
ClickDetector.MouseClick:Connect(function()
    if not debounce then
        debounce = true
        local cloneNoob = NPC:Clone()
        cloneNoob.Name = "Noob"
        cloneNoob.Parent = NPCSpawn
        part.SurfaceGui.TextLabel.Text = "Cooldown..."
        part.Color = Color3.fromRGB(255,0,0)
        wait(30)
        part.SurfaceGui.TextLabel.Text = "Spawn"
        part.Color = Color3.fromRGB(0,255,0)
        cloneNoob.Humanoid.Health = 0
        wait(10)
        cloneNoob:Destroy()
        print("Cooldown Completed!")
        local cloneMachine = newMachine:Clone()
        cloneMachine.Name = "SpawnMachine"
        cloneMachine.Parent = workspace
        print("New Machine Has Been Spawned!")
        wait(0.001)
        part.Parent:Destroy()
        print("Old Machine Has Been Destroyed!")
    end
	print("Clear to press")
	debounce = false
end)

Try that and don't spam the button just press it 1 time wait for it to print clear to press then click it again. Then go from there. I am not in studio so I can't test this but It should work.

6 comments

0

Don't put that code on line 1 past it on line 9 and remove your old code keep all of the local stuff. Be sure to copy the code by clicking view source.

0

Local stuff i.e. lines 1-8.

0

In the 3rd time, the machine disappered forever

0

Can you fix that?

0

Tbh that's beyond my knowledge I just started scripting 4 days ago. That thing I told you has always worked for me.

0

nvm I just accpet your

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