Hello , i have been trying to figure out for quite a while as to why my value changement wasn’t detected, i tried manually changing it and there’s no prints at all. The value is a NumberValue.
(Sorry for the messy script paste, i really don’t know how to put the whole script in a blockquote)
–Server Script in ServerScriptService
local available ={1,2,3,4,5}
local Seat1 = game.Workspace.SofaPartClub.Seat1
game.Players.PlayerAdded:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerV = Character:WaitForChild(“PlayerV”)
PlayerV.Value = available[1]
table.remove(available,1)
print(Character.PlayerV.Value)
print(“gave player number”)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Character = Player.Character or Player.CharacterAdded:Wait()
table.insert(available, Character.PlayerV.Value)
end)
local script in StarterGui
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Seat1 = game.Workspace.SofaPartClub.Seat1
local PlayerV = Character:WaitForChild(“PlayerV”)
PlayerV:GetPropertyChangedSignal(“Value”):Connect(function()
print(“IT CHANGED.”)
if PlayerV.Value == “1” then
print(“ya”)
Seat1:Sit(Player.Humanoid)
end
end)
1 Like
Where is the PlayerV coming from. You haven’t created it in any scripts provided.
Use ``` for script blockquotes.
From what it looks like, you’re using a number value for your value. The script runs when it detects a string value, which is not what you want.
Use this:
if PlayerV.Value == 1 then
print(“ya”)
Seat1:Sit(Player.Humanoid)
end
He did create it, he just didn’t use script blockquotes.
I’ve put the code into Studio, and there’s no creation of PlayerV but lets assume that it has been created elsewhere. Additionally, the :GetPropertyChangedSignal()
is not firing in the first place, so that may be the source of error.
I’ve noticed that you are using :GetPropertyChangedSignal()
rather than the built-in .Changed()
event. I don’t think, from seeing your issue, that :GetPropertyChangedSignale()
will fire on a value object. Try changing it to that.
Additionally, you should use
local character = player.Character or player.CharacterAdded:Wait()
in order to wait for the character if it has not loaded yet.
Furthermore, Humanoid
is a child of the Character
and not the Player
That should hopefully fix any issues.
According to this bit, there is a value. It’s located in the player’s character.
And yes, Changed()
would work, but would activate each time the object’s properties change, which takes up memory and is inefficient. In general, GetPropertyChangedSignal()
is more efficient since it fires only when a specific property gets changed.
https://developer.roblox.com/en-us/api-reference/event/StringValue/Changed
.Changed()
is only fired when the value of the object is changed, and is designed for this explicit reason and even passes the new value.
I’ll try out what u told be also yeah the player.humanoid is cause i have been scripting since the morning so I’ve gotten quite tired, thanks for the tips, I’ll tell if it works or not tomorrow
So there’s no need to detect changes for the number value? (Nvm)
I understand that, but do you really want it to fire every time a property gets changed when you only need one event for one property? This is my point of why in this scenario, Changed()
is less efficient than GetPropertyChangedSignal()
. However, if it were for multiple values, then it would be more efficient since that means less connections, and cleaner script.
I don’t quite understand your reasoning. They both fire when the value of the object changes, and only when the value changes. They are mostly the same, but I would assume that .Changed()
is optimised for this specific change to the .Value
property.
Neither event should fire more or less often than the other.
.Changed() Is the same thing as getpropretychangedsignal the only difference is that :GetPropretyChangedSignal gets a specific value in the numberval
Are you referring to this line of code?
if PlayerV.Value == "1" then
print("Correctly changed.")
Seat1:Sit(Character.Humanoid)
end
If so, then I agree in keeping it, I’m just changing the connection line.
Okay. So, let’s say that I change two values with Changed()
:
local part = workspace.Part
part.Changed:Connect(function()
print("A value was changed.")
end)
part.Transparency = 0.5
part.CanCollide = false
--this will fire multiple times
This would fire multiple times.
And, according to Instance.Changed, this code example from it would fire multiple times in one change.
part.Changed:Connect(print)
-- This fires Changed with "Transparency"
part.Transparency = .5
-- Similarly, this fires Changed with "Number"
However, if I were to use GetPropertyChangedSignal()
using the code I put earlier, it would only have to fire once:
local part = workspace.Part
part:GetPropertyChangedSignal("Transparency"):Connect(function()
print("A value was changed.")
end)
part.Transparency = 0.5
part.CanCollide = false
--this will fire only when Transparency changes
This once again, explains my point. In this scenario, when we only need to check one value, Changed()
is not needed.
1 Like
Well daiytheghostchild98 made be notice i put ’ “1” ’ instead of ’ 1 ’
Also, that if statement is wrong if you use a string. Remember, OP is using a NumberValue, and the script is checking for a string, instead of a number.
Hence, what I said earlier:
1 Like
Fired whenever the StringValue.Value
of the StringValue
is changed. It will run with the new value being stored in the argument object, instead of a string representing the property being changed.
Taken from the Developer page. Have you tested that code, because I tried changing the name of the StringValue and it did not fire. As the documentation says, it is the event that fires when the Value of the property is changed, not any property like other Instances.
That’s automatically formatted in Discourse. It shouldn’t cause an issue unless you actually put it in your code. Also, ‘’ is the same as “”.
Now, this is where I don’t understand your reasoning. OP says in his question that he uses a NumberValue:
But, you want to check with a string, when using a number will still do the same thing? I’m confused.
Also, where did you get StringValue.Name
from? OP clearly uses .Value
in his code:
You can use either one as long as you are passing the right types. And it shouldn’t make any differences. If you use a StringValue check for equalling “1” and if you’re using a NumberValue check for equalling 1.
I didn’t use StringValue.Name
anywhere else, but if I were to change its name when a .Changed()
event is connected, it would not fire.