How do I change a Value when another Value is a certain number?

Hello! I am working on a game and I need to make it so that every-time a Value (Qualed) equals 15, then change gTime to 0. I have been having trouble with this for few days now and cant make progress, please help!

local gTime = game.Workspace.gTime.Value


-- How many players need to be Completed
local MinQual = script.Parent.MinQual
local Qualed = script.Parent.Qualed
--
script.Parent.Touched:Connect(function()

	Qualed.Value = Qualed.Value + 1
	print(Qualed.Value)

	if Qualed.Value == 15 then
		gTime = 0
		
	end

	
end)
1 Like

You would want to use a .Changed event for that.

I.e

Qualed.Changed:Connect(function()
    if Qualed.Value == 15 then
        gTime = 0
    end
end

Hope this helps!

1 Like

Hello! Thank you for your response although, there is one problem. The script will run without errors but, nothing happens when I get to 15

This is my script:

local gTime = game.Workspace.gTime.Value


-- How many players need to be Completed
local MinQual = script.Parent.MinQual
local Qualed = script.Parent.Qualed
--
script.Parent.Touched:Connect(function()

	Qualed.Value = Qualed.Value + 1
	print(Qualed.Value)


end)

Qualed.Changed:Connect(function()
	if Qualed.Value == 15 then
		gTime = 0
	end
end)
1 Like

You are storing the gTime Value in a variable, rather than the gTime object. Because of this, when you update the gTime variable, you aren’t actually updating the gTime object.

Try this: (notice changes on first line and third to last line)

local gTime = workspace

-- How many players need to be Completed
local MinQual = script.Parent.MinQual
local Qualed = script.Parent.Qualed
--
script.Parent.Touched:Connect(function()
	Qualed.Value += 1
	print(Qualed.Value)

	if Qualed.Value == 15 then
		gTime.Value = 0
	end
end)

I also removed the Changed, as it is redundant (assuming that this Touched event is the only thing changing Qualed value)

2 Likes

Hello! Thanks for responding but, again there is nothing happening. no errors, no problems, just not working?

1 Like

I created a setup that is the same as what I believe yours is, and it worked fine. Could you maybe show a image of your explorer view?

1 Like

It might not seem like it works for you because your number value is 0 as default. Meaning that you can’t see if it has changed or not. Or maybe it’s not.

You should try printing out “gTime.Value” and see it’s equal to 0. Make sure the touched event is working properly.

image

Does anything print when the part is touched? It should print the value of Qualed.

Yes, it prints the value of Qualed.

Is the starting value of Qualed above 15? If it never reaches 15 because it is already above 15 then gTime will never go to 0.

here let me help you out. First, let’s make whats called a closer. This is a simple object that will manage yourself that you can reuse if you feel.

‘’’
local function OnValueReset(resetVal)
–ref to our world clock not the value if you pull
–the value your just copying whats inside not the obj
local gTime = game.workspace.gTime

local resetVal = resetVal
local currentCount = 0


-- for safety reason lets add a debounce cool down
-- as we know touched can fire an event a crazy number of times 

local isCoolDown = false


 --when we call this and pass a reset val

–We are creating a new environment and this one
–would be unique to any others now since we would all --be pointing to qTime if you make multiple just
–remember they won’t be sharing their own increments

return function()
if not isCoolDown then
isCoolDown = true
– there are lots of ways to do this i did it this way
– for speed and im doing this from my phone
spawn(function()
wait(0.5)
isCoolDown = false
end)
currentCount = 1 + currentCount
if currentCount >= resetVal then

            gTime.Value = 0
            currentCount = 0

          end
     end
end

end

– alright done now let’s use this

– we create our iterator with a 15 set proc
local OnTouchedTracking = OnValueReset(15)

– we hook it up to be called on touch
script.Parent.Touched:Connect(OnTouchedTracking)

–This will iterate to your reset value perform reset and
–then do it again.
–If you create a new on though you must treat it as a
–whole different iterator not sharing values. If you want
–itnto share values you would ref something outside of --it like gTime or local to that script.

‘’’

I added a debounce, just because its the right thing to do with touch events.

The advantages of doing it this way are now you have a clear logic processing zone for your iterator and if you want to create other iterators that could be used by different events and have different reset values you can.
But remember they dont share an iterator unless you ref one outside of the function return encapsulated area.

Sorry did this from my phone

It starts at 0, and every-time the finish is touched + 1.

Can you double check to make sure gTime is actually not being updated to 0? Make sure its default value is not already 0, because then you won’t see a difference.

The status GUI is always updated to equal gTime.

Using the explorer and properties menu, can you confirm that it is not changing? It is possible the issue here has to do with the code your GUI is using to display gTime.

local gTime = game.Workspace.gTime

-- How many players need to be Completed

local MinQual = script.Parent.MinQual

local Qualed = script.Parent.Qualed

--

script.Parent.Touched:Connect(function(hit)

local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

if player then

Qualed.Value = Qualed.Value + 1

print(Qualed)

end

end)

Qualed:GetPropertyChangedSignal("Value"):Connect(function()

if Qualed.Value == 15 then

gTime.Value = 0

print(gTime.Value)

end

end)

You are right! gTime is being set to 0! The GUI just wont show it.

Your GUI script might be suffering the same problem as this script did. Make sure you are not storing the gTime value in a variable, but rather storing the gTime object and then doing gTime.Value wherever you are currently doing gTime. If you’d like me to take a look, you can post it below.

Make sure to mark the solution above as the correct one if anyone else has the same problem.

What do you mean by “gTime object”?