Number value in script resetting without reason

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Im trying to have mode change to number 2 after number 1 is used and THEN rese

  2. What is the issue? Include screenshots / videos if possible!
    for some reason the mode keeps reseting back to 1, i even added a print and it is setting mode to 2, but once i use the script again, it resets to 1. the weird thing is, the value isnt inside of an event, its sitting at the top of the script

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    ive tried removing the thing that resets it, but that dosent even get fired to begin with

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local event = script.Parent.AbilityEvent
local savedCFrame = nil
local mode = 1
event.OnServerEvent:Connect(function(player, equipped)-- what makes ability run
	 if script.CanUse.Value == true then
	local char = script.Parent.Parent
	local glove = script.Parent
	if glove.Parent.Name ~= "Backpack" then
	local AbilityName = script.Parent.AbilityUseName
	print(glove.Parent.Name, "Used", AbilityName.Value)
	-- required variables
		local HRP = char.HumanoidRootPart
		print(mode)
		script.Enabled = false
		if mode == 1 then
			savedCFrame = HRP.CFrame
			glove.MeshPart.BeBack:Play()
			mode = 2 
			print(mode)
		elseif mode == 2 then
				glove.MeshPart.Us:Play()
				game.ReplicatedStorage.AbilityClient:FireClient(game.Players:GetPlayerFromCharacter(char), "Thanos")
				task.wait(1.6)
			char:PivotTo(savedCFrame)
			savedCFrame = nil
			--mode = 1
			print(mode.. " Reset")
		end
		
		
			local player = game.Players:GetPlayerFromCharacter(char)
			local cdtime = script.Cd.Value
			local green = player.PlayerGui.Cooldown.Green
			local function Cooldown()
				player.PlayerGui.Cooldown.Enabled = true
				local ts = game:GetService("TweenService")
				local ti
				ti = .2
				green:TweenSize(UDim2.new(0,0  ,   0,22), "In", "Quad", ti)
				wait(.2)
				ti = cdtime - .2
				green:TweenSize(UDim2.new(0,170  ,   0,22), "In", "Quad", ti)
				wait(cdtime - .2)
				player.PlayerGui.Cooldown.Enabled = false
			end

			Cooldown()
		script.Enabled = true
		
		
		end
		end
end)-- end of ability


this is the full script.
I’d like any reason and/or fix to this

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Maybe the script is reset somehow each time? (by deleting and cloning it, or by resetting if the script is in StarterCharacterScripts). Speaking of, where is the script located?

Also, I’m interested in why you use script.Enabled to pause the script. Are you perhaps looking for a debounce instead? An example in your case:

local cooldown = false
event.OnServerEvent:Connect(function(player, equipped)
	if cooldown == true then return end -- Stop the code if on cooldown
	if script.CanUse.Value == true then
		...
		if glove.Parent.Name ~= "Backpack" then
			...
			local function Cooldown()
				cooldown = true
				...
				cooldown = false
			end
			Cooldown()
		end
	end
end)

i think that it resets because of the garbageCollector you can add the mode variable in a module script/table to prevent it from resetting smt like this

local tbl ={mode = 1}

amnot sure if that will fix it

I have certain reasons for disabling the script that are pretty complicated to get into.

But as for the script, its sitting inside of a tool, it dosent get cloned or anything, same tool, same script and all. it just randomly resets,

1 Like
local event = script.Parent.AbilityEvent
local savedCFrame = nil
local tbl = {mode=1}
event.OnServerEvent:Connect(function(player, equipped)-- what makes ability run
	 if script.CanUse.Value == true then
	local char = script.Parent.Parent
	local glove = script.Parent
	if glove.Parent.Name ~= "Backpack" then
	local AbilityName = script.Parent.AbilityUseName
	print(glove.Parent.Name, "Used", AbilityName.Value)
	-- required variables
		local HRP = char.HumanoidRootPart
			print(tbl.mode)
		script.Enabled = false
		if tbl.mode == 1 then
			savedCFrame = HRP.CFrame
			glove.MeshPart.BeBack:Play()
				tbl.mode = 2 
				print(tbl.mode)
			elseif tbl.mode == 2 then
				glove.MeshPart.Us:Play()
				game.ReplicatedStorage.AbilityClient:FireClient(game.Players:GetPlayerFromCharacter(char), "Thanos")
				task.wait(1.6)
			char:PivotTo(savedCFrame)
			savedCFrame = nil
			--mode = 1
				print(tbl.mode.. " Reset")
		end
		
		
			local player = game.Players:GetPlayerFromCharacter(char)
			local cdtime = script.Cd.Value
			local green = player.PlayerGui.Cooldown.Green
			local function Cooldown()
				player.PlayerGui.Cooldown.Enabled = true
				local ts = game:GetService("TweenService")
				local ti
				ti = .2
				green:TweenSize(UDim2.new(0,0  ,   0,22), "In", "Quad", ti)
				wait(.2)
				ti = cdtime - .2
				green:TweenSize(UDim2.new(0,170  ,   0,22), "In", "Quad", ti)
				wait(cdtime - .2)
				player.PlayerGui.Cooldown.Enabled = false
			end

			Cooldown()
		script.Enabled = true
		
		
		end
		end
end)-- end of ability


i tried it using a table, nothing changed

ok maybe add the mode variable as a number attribute to the script
attributes shouldnot reset

if the attribute reseted then smt in your script resets the value

it appears i also need to do this with the saved cframe as its decided that everything in the script resets.

but this did work, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.