An exact value inside the Loop (RunService) is not being changed, i need help!

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

  1. What do you want to achieve?
    Fix

  2. What is the issue?
    The problem I have is that the NewTime value is not changed inside the loop, but it is changed outside. The odd thing is that the “Acumulado” value does change within the loop when called again by the function

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to look for solutions, but my inglish is not the best so i dont found anything that helped me out.

local FueGolpeadoDeNuevo = false
local Velocidad = 0.1 -- 10 veces 1 segundo
local Acumulado = 0

function StunModuleHandler.Stun(User, Time, WalkSpeed)
	local Humanoid = User:WaitForChild("Humanoid")
	local Stunear = StunModuleHandler.BuscarEnTabla(User) --Searchs on a table if player is stunned
--returns "YaEstaStuneado" if hes on the table
--returns "Stunear" if we need it to stun him
	local NewTime = Time
	local desconectar = false



	
	local function RemoveFromTable(User, disconectar) --Removes player from table of stunneds
		disconectar:Disconnect()
		for x, Usuarios in pairs(Stuneados) do
			if Usuarios and Usuarios[User] then
				print(Stuneados)
				table.remove(Stuneados, x)
				print("El usuario pa")
				print(Stuneados)
			end
		end
	end
	
	if Stunear == "YaEstaStuneado" then-- If player is already on table, then 
		FueGolpeadoDeNuevo = true
		if FueGolpeadoDeNuevo then
			NewTime = Time  -- gives new time???
			Acumulado = 0  -- Resets acumulated time
			print(Acumulado, NewTime, "Fue Golpeado")
			FueGolpeadoDeNuevo = false
		end
	end

	if Stunear == "Stunear" then --Se stunea el jugador y se va restando el tiempo
		local dis
		task.spawn(function()	
			dis = Runs.Heartbeat:Connect(function(deltatime)
				Acumulado = Acumulado + deltatime
				print(Acumulado, NewTime)  --<<< this is what should update
				if Acumulado >= NewTime then
					print("¡Tiempo agotado!")
					desconectar = true 
					RemoveFromTable(User, dis)
				end	
			end)
		end)		
		while not desconectar do
			wait(0.1) 
		end

		Acumulado = 0
		dis:Disconnect()
		print("Desconectado")
	end
end

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.

2 Likes

The reason why the NewTime value isn’t changing is because you’re not updating its value within a loop, its value is only being updated whenever the StunModuleHandler.Stun function is called

could be that you’re not declaring NewTime globally

local FueGolpeadoDeNuevo = false
local Velocidad = 0.1
local Acumulado = 0
local NewTime

function StunModuleHandler.Stun(User, Time, WalkSpeed)
    local Humanoid = User:WaitForChild("Humanoid")
    local Stunear = StunModuleHandler.BuscarEnTabla(User)
    NewTime = Time
    local desconectar = false

    local function RemoveFromTable(User, disconectar)
        disconectar:Disconnect()
        for x, Usuarios in pairs(Stuneados) do
            if Usuarios and Usuarios[User] then
                table.remove(Stuneados, x)
            end
        end
    end

    if Stunear == "YaEstaStuneado" then
        FueGolpeadoDeNuevo = true
        if FueGolpeadoDeNuevo then
            NewTime = Time
            Acumulado = 0
            FueGolpeadoDeNuevo = false
        end
    end

    if Stunear == "Stunear" then
        local dis
        task.spawn(function()    
            dis = Runs.Heartbeat:Connect(function(deltatime)
                Acumulado = Acumulado + deltatime
                if Acumulado >= NewTime then
                    desconectar = true 
                    RemoveFromTable(User, dis)
                end 
            end)
        end)        
        while not desconectar do
            wait(0.1) 
        end

        Acumulado = 0
        dis:Disconnect()
    end
end
2 Likes

As a precaution I would suggest using local NewTime = 0 instead of local NewTime since it’s a number value, otherwise there’s a small chance that you’ll get the error attempt to compare nil <= number if the loop happens to start before the NewTime value is set

1 Like

Yeah, It’s a punch system, each punch has a different stun (time), so yes, it changes every time it’s called!
M1; 3
M2; 5
–Big seconds for the test, but yeah. It print a different time everytime the stun is called, but it just keeping the first time of the M1

1 Like

Alrighty, ill change that on the code!

I edited the way the loop disconnects, now it should be able to disconnect itself which might help with the problem:

local FueGolpeadoDeNuevo = false
local Velocidad = 0.1 -- 10 veces 1 segundo
local Acumulado = 0

function StunModuleHandler.Stun(User, Time, WalkSpeed)
	local Humanoid = User:WaitForChild("Humanoid")
	local Stunear = StunModuleHandler.BuscarEnTabla(User) --Searchs on a table if player is stunned
	--returns "YaEstaStuneado" if hes on the table
	--returns "Stunear" if we need it to stun him
	local NewTime = Time
	local desconectar = false




	local function RemoveFromTable(User, disconectar) --Removes player from table of stunneds
		disconectar:Disconnect()
		for x, Usuarios in pairs(Stuneados) do
			if Usuarios and Usuarios[User] then
				print(Stuneados)
				table.remove(Stuneados, x)
				print("El usuario pa")
				print(Stuneados)
			end
		end
	end

	if Stunear == "YaEstaStuneado" then-- If player is already on table, then 
		FueGolpeadoDeNuevo = true
		if FueGolpeadoDeNuevo then
			NewTime = Time  -- gives new time???
			Acumulado = 0  -- Resets acumulated time
			print(Acumulado, NewTime, "Fue Golpeado")
			FueGolpeadoDeNuevo = false
		end
	end

	if Stunear == "Stunear" then --Se stunea el jugador y se va restando el tiempo
		local dis
		task.spawn(function()	
			dis = Runs.Heartbeat:Connect(function(deltatime)
				Acumulado = Acumulado + deltatime
				print(Acumulado, NewTime)  --<<< this is what should update
				if Acumulado >= NewTime then
					print("¡Tiempo agotado!")
					desconectar = true 
					RemoveFromTable(User, dis)
					Acumulado = 0
					dis:Disconnect()
					print("Desconectado")
				end	
			end)
		end)
	end
end
1 Like

This worked, and i dont know why but thank you!

But now, my question is, if another player uses the function, that time would be the same for him, no? Everything who isnt inside of the function whould be the same for everyone?

The script goes like this.

Client → Server → ModuleScript

It disconnects! Check the last comments i did after this, i asked some questions ty :smile:

1 Like

To solve this problem you can make the NewTime into a table and use the player’s UserId as the index. You’ll need to pass the player’s UserId to your Stun function for this to work though

1 Like

Alright, Ill try! Thx so much!

1 Like

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