So I don't know if .Changed is only for client side or is for both If it is only client side then what can I use instead?
function Check() if rmVar == 3 or 4 then if game.ReplicatedStorage.PlayersAlive.Value == 1 or 0 then EndGame() Intermission() end elseif rmVar == 1 or 2 then if game.ReplicatedStorage.PlayersAlive.Value == 0 then EndGame() Intermission() end end end game.ReplicatedStorage.PlayersAlive.Changed:Connect(Check)
function Check() if rmVar == 3 or rmVar == 4 then if game.ReplicatedStorage.PlayersAlive.Value == 1 or game.ReplicatedStorage.PlayersAlive.Value == 0 then EndGame() Intermission() end elseif rmVar == 1 or rmVar == 2 then if game.ReplicatedStorage.PlayersAlive.Value == 0 then EndGame() Intermission() end end end game.ReplicatedStorage.PlayersAlive.Changed:Connect(Check)
This answer's to be used as a reference
When coming into the if
statement, some users do the following.
if Val == 1 or 0 then -- Code end
Just by reading, what do you see? If it's still a bit hard to see, let's make it a bit more clear.
if (Val == 1) or 0 then -- Code end
The issue's that it's not saying "if Value is equal to 1 or is equal to 0, then...", it's actually saying "if Value is equal to 1 or 0 is true, then..." Since numbers return truthy, if the first value's not 1
, it will go to 0
and see that it's truthy, thus fire the following code.
The solution's to check the value individually, for example;
if Val == 1 or Val == 2 then -- Code end
Here, it will check if Val
is equal to 1 or if it's equal to 0.
Stuff touched on but not really explained
Truthy
- Incapax did a good explanation of this. In a nutshell, values such as numbers are considered truthy
when being evaluated.
If Statement
- Evaluates a value whether it's true or false. (You probably know that though lol.)
If you have anymore questions, please let me know. :)
Changed is an if statement bundled. Basically it asks whether or not the connected property Changed, if so, do whatever asked afterward. This can be viewed like so, this example will take a Boolean instance changing from either set choice.
local Boolean = script:WaitForChild("Boolean").Value spawn(function() while wait(2.5) do Boolean = (not Boolean) end end) Boolean.Changed:Connect(function() print(Boolean) end)
Yet .Changed
is too unspecific, there is a new method called ‘GetPropertyChangedSignal()
’ that’s a lot more efficient and direct with the property; this give the ability to fire this event more restrictively towards one property instead of running if any change, ultimately providing more choice. it’s best to write this like so, yet there are other methods too:
local Boolean = script:WaitForChild("Boolean").Value spawn(function() while wait(2.5) do Boolean = (not Boolean) end end) local function PrintOutcome() print(Boolean) end PrintOutcome() Boolean:GetPropertyChangedSignal("Value"):Connect(PrintOutcome)
Proper formatting of this method:
Instance:GetPropertyChangedSignal(“Property”):Connect(SomeFunction)
If you’d like to learn more, see this website https://developer.roblox.com/api-reference/function/Instance/GetPropertyChangedSignal
Fix:
local PlayersAlive = game:GetService(“ReplicatedStorage”):WaitForChild(“PlayersAlive”).Value local function Check() if rmVar == 3 or 4 then if PlayersAlive == 1 or 2 then EndGame() Intermission() end elseif rmVar == 1 or 2 then if PlayersAlive < 1 then EndGame() Intermission() end end end Check() PlayersAlive:GetPropertyChangedSignal(“Value”):Connect(Check)