What do you want to achieve? Keep it simple and clear!
i’d like to know why my if statement isn’t working
What is the issue? Include screenshots / videos if possible!
the if statement i’ve made doesn’t detect if the object value isn’t nil
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
couldn’t find any solutions so far
here’s the script
script.Parent.Parent.currentMatchMaker:GetPropertyChangedSignal("Value"):Connect(function()
if script.Parent.Parent.currentMatchMaker.Value ~= "" or script.Parent.Parent.currentMatchMaker.Value ~= nil then
script.Parent.Parent.currentMatchMaker.Value.timeLeft.Changed:Connect(function()
script.Parent.Value = script.Parent.Parent.currentMatchMaker.Value.timeLeft.Value
end)
end
end)
that’s because currentMatchMaker is a object value. in order to get the right timeleft for that currentmatchmaker i have to access the value since the value has timeleft as a child.
my script works fine when the currentmatchmaker has a value
I believe the first condition is the culprit. You should not try to compare objects and strings. Better do this:
if not (script.Parent.Parent.currentMatchMaker.Value == nil) then
script.Parent.Parent.currentMatchMaker.Value.timeLeft.Changed:Connect(function()
script.Parent.Value = script.Parent.Parent.currentMatchMaker.Value.timeLeft.Value
end)
end
Not to mention, an empty string isn’t the same as a nil value. Nil means it is a completely empty value, not even 0, while an empty string is a string value without characters.
The issue was that the function was giving nil values since the value of currentMatchMaker was nil and its because that function was still connected and wasn’t disconnected so, it just kept running without the if statement being ran again. if i were to use RBXScriptSignal:Once() then it would only run once and wouldn’t update what i wanted it to do is let the function connected when currentMatchMaker has a value.
My solution was to disconnect the function thing when the value of currentMatchmaker is nil.
Fixed version with no errors
local connection
script.Parent.Parent.currentMatchMaker:GetPropertyChangedSignal("Value"):Connect(function()
if script.Parent.Parent.currentMatchMaker.Value then
connection = script.Parent.Parent.currentMatchMaker.Value.timeLeft.Changed:Connect(function()
script.Parent.Value = script.Parent.Parent.currentMatchMaker.Value.timeLeft.Value
end)
elseif not script.Parent.Parent.currentMatchMaker.Value then
connection:Disconnect()
end
end)