Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I use .Changed in a scipt??

Asked by
Vain_p 78
5 years ago

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)
0
use changed serversided Imperialy 149 — 5y
0
intvalue.Changed:Connect(function() if intvalue.Value > 50 then print('greater than 50') end end) Imperialy 149 — 5y
0
What do you mean? Vain_p 78 — 5y

3 answers

Log in to vote
-2
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
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)
0
.Changed is for both I believe, however I usually do it in client-side. Mr_Unlucky 1085 — 5y
0
the script did not work :/ Vain_p 78 — 5y
1
if the script did not work why is it accepted? HappyTimIsHim 652 — 5y
0
^ TheeDeathCaster 2368 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

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

0
It's been a little since I've answered a question, so please go easy on me. c; TheeDeathCaster 2368 — 5y
0
I have already tried that did not work :/ Vain_p 78 — 5y
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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)
0
there are multiple changes to this answer, everything should be good now Ziffixture 6913 — 5y
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 Vain_p 78 — 5y
0
Try to follow the method I’ve provided, if you’re still having issues, just comment again Ziffixture 6913 — 5y
0
I did still no luck Vain_p 78 — 5y
View all comments (5 more)
0
Could you paste the script above? It’s harder to read in comments, I’ll fix it up for you with this method Ziffixture 6913 — 5y
0
Oh I see it is already there, I’ll get to it, hold on... Ziffixture 6913 — 5y
0
There, I posted it at the bottom of my answer, hope it works, if not I’ll take another look Ziffixture 6913 — 5y
0
Did it work? Ziffixture 6913 — 5y
0
I got it to work I put it on the top of my script instead of the buttom Vain_p 78 — 5y

Answer this question