Script keeps looping even after being destroyed

Hi people!

I’m having an issue where even after a script has been destroyed, the loops inside continues to run.

I’ve tried parenting the script to server storage THEN destroying it, but the outcome is still the same. I’ve also tried adding an if statement inside the loop checking if the folder that the script is in equals a specific name and if it doesn’t then it breaks the loop, but that also doesn’t work either.

Here’s the script I want to delete

how can i delete a script without having the code inside keep running

Can I see the grow module script? Maybe the problem is coming from there

1 Like

The code would normally stop running, may I see how are you deleting it?

1 Like

I’m new to scripting so if it’s messy or there’s a better way to do it I’m sorry lol

local GrowModule = {}

local function updateGui(player,creature)
	local waterGuiModule = require(player.PlayerGui:WaitForChild("PlayerGuiStats"):WaitForChild("Stats"):WaitForChild("Thirst"):WaitForChild("Blue").WaterGuiModule)
	local foodGuiModule = require(player.PlayerGui:WaitForChild("PlayerGuiStats"):WaitForChild("Stats"):WaitForChild("Hunger"):WaitForChild("Red").FoodGuiModule)
	waterGuiModule.ChangeText(player,creature)
	foodGuiModule.ChangeText(player,creature)
end

local function growFunction(player,age,rate,creatureSelected,perc,amber)
	local isAdult
	if player.Character then
		local stage
		local model
		local pos = player.Character:GetPrimaryPartCFrame()
		local teenAmber = rate * 3
		local adultAmber = rate * 4
		local servCreature = game.ServerStorage:WaitForChild("Assets"):WaitForChild("Morphs"):WaitForChild(creatureSelected)
		
		if age.Value == "Adult" then
			isAdult = true
		end
		
		if isAdult then return end
	
		perc.Value = 0
		local growScript = player.Character.CreatureExtras.Scripts.GrowScript
		growScript.Parent = game.ServerStorage.GarbageContainer
		growScript:Destroy()
		player.Character:Destroy()
	
		if age.Value == "Baby" then
			amber.Value = amber.Value + teenAmber
			model = servCreature:WaitForChild("Teen"):Clone()
			age.Value = "Teen"
		elseif age.Value == "Teen" then
			amber.Value = amber.Value + adultAmber
			model = servCreature:WaitForChild("Adult"):Clone()
			age.Value = "Adult"
		end
		model.Name = player.Name
		player.Character = model
		model.Parent = workspace.Players
		model:SetPrimaryPartCFrame(pos)
		local module = require(player.PlayerGui.UserSelectionGui:WaitForChild("GUI_Left").GrowthCombat.GreenContainer.GrowthText.GrowthGuiModule)
		updateGui(player,creatureSelected)
		module.ChangeText(perc.Value,age.Value)
		local nameTagModule = require(game.ServerStorage:WaitForChild("Modules"):WaitForChild("UpdateNameTags"))
		nameTagModule.update()
	end
end


function GrowModule.percUp(player,age,rate,creature)
	if player.Character.Parent.Name == "Players" then
		local creatureValues = game.ServerStorage:WaitForChild("ServerPlayerStats"):WaitForChild(player.Name):WaitForChild("CreatureFolder"):WaitForChild(creature)
		local perc = creatureValues:WaitForChild(creature.."-Percent")
		local servAge = creatureValues:WaitForChild(creature.."-Age")
		local amber = creatureValues.Parent:WaitForChild("Amber")
		local rateTwo = player.Character.CreatureExtras.Stats.Rate.Value
			if perc.Value < 100 then
				perc.Value = perc.Value + 1
				if perc.Value >= 100 then
					growFunction(player,servAge,rateTwo,creature,perc,amber)
				end
			end
		wait(.5)
		local guiModule = require(player:WaitForChild("PlayerGui").UserSelectionGui:WaitForChild("GUI_Left").GrowthCombat.GreenContainer.GrowthText.GrowthGuiModule)
		guiModule.ChangeText(perc.Value,servAge.Value)
	end
end

return GrowModule

1 Like

And you are 100% script is no longer there - have you checked the explorer?

2 Likes

I believe when you destroy the player’s character, if GrowScript is located in StarterCharacterScripts, it just gets re-created so maybe you should first destroy the character and then destroy the script, or well… I don’t quite know what you’re going for…
if it’s not the “GrowScript” in question then just let me know which one it is
EDIT: I could be wrong though

2 Likes

Yup, all player’s characters are parented to a folder in workspace called Players, and on both the server and client the orignal player’s character isn’t in there anymore. also there’s no other scripts that mess with these ones either.

There’s a folder in server storage that holds all the creatures models. 3 different sizes with their own respective stats for each creature. In each creature there’s a script folder and the growscript is in there. My players are running into an issue where when you grow then switch to a different creature it continues to increase the original creature’s percentage along with the current creature’s percentage at the same time.

https://gyazo.com/979bcdd7270cbc708620068a48b68cb6

1 Like

Hmm well the reason might be that destroying objects parents them to nil, how about you try disabling it instead?

2 Likes

I’ve tried disabling the growscript in the module script once the player grows into the next stage, but it still has the same behavior. Scripting is confusing

did you make sure to check if it’s disabled or not, destroyed or not in the explorer tab?

1 Like

it’s not in the explorer at all, that’s what I’m so confused about

Where did you add the if statement to break the loop?

1 Like

I believe it’s because the values stay the same, did you reset them every time the player changes their creature?

1 Like

You could as well use coroutines here and pause it once you want it to stop? I’m new to coroutines so I wouldn’t exactly know how to do that.

1 Like

In the original screenshot I provided in the post

What values? I’m not following

the age and whatever other growth value you have, I didn’t really check in too much

1 Like

I also want to point at that this ONLY started happening when I moved from local scripts and server events to server scripts and modules. I had to switch because exploiters were insta-growing to adult.
The scripts were the same but changed up to require a module instead of firing a server event.

As I mentioned it earlier already, script is only getting parented to nil, but it is still running. In order to break the while loop you could wrap it either in a function and disconnect it or coroutine and pause it.

1 Like