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)
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)
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)
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.
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.
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.
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)
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.