Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I use .Changed in a scipt??

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)

3 comments

0

use changed serversided

0

intvalue.Changed:Connect(function() if intvalue.Value > 50 then print('greater than 50') end end)

0

What do you mean?

3 answers

✓ Accepted answer
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)

4 comments

0

.Changed is for both I believe, however I usually do it in client-side.

0

the script did not work :/

1

if the script did not work why is it accepted?

0

^

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

  1. Truthy - Incapax did a good explanation of this. In a nutshell, values such as numbers are considered truthy when being evaluated.

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

2 comments

0

It's been a little since I've answered a question, so please go easy on me. c;

0

I have already tried that did not work :/

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)

9 comments

0

there are multiple changes to this answer, everything should be good now

0

this wis the script I have now and it still does not work function Check() print("Oktest") if rmVar == 3 then print("Testies") if game.ReplicatedStorage.PlayersAlive.Value == 1 then EndGame() Intermission() elseif game.ReplicatedStorage.PlayersAlive.Value == 0 then EndGame() Intermission() end elseif rmVar == 4 then if game.ReplicatedStorag

0

Try to follow the method I’ve provided, if you’re still having issues, just comment again

0

I did still no luck

0

Could you paste the script above? It’s harder to read in comments, I’ll fix it up for you with this method

0

Oh I see it is already there, I’ll get to it, hold on...

0

There, I posted it at the bottom of my answer, hope it works, if not I’ll take another look

0

Did it work?

0

I got it to work I put it on the top of my script instead of the buttom

This discussion is preserved as it was. New answers, comments, and votes are closed.