Hello I am curious about how to see how much of the value has changed. For example, I currently have 30 points, but when I touch a block I get 20 points, totaling my stats to 50 points. How can I do it to the script can detect the changed value. So in short term, how could I make it so the script prints out 20. Is using GetPropertyChangedSignal an option? How would I do it?
There are 2 events you can use to check if a Value has changed, .Changed and :GetPropertyChangedSignal(). GetPropertyChangedSignal can work for any other object, while the Changed event is only for Value objects. I don’t think it matters which event you use to detect when the Value changes, but I’d prefer using GetPropertyChangedSignal.
From what I see, it appears you want to check how many points have been added. Now, if you want to do this, there is one way I know. I will give a code example of how you can make this with some explanation (GetPropertyChangedSignal will be used).
Code example:
--[[
So let's say this is a Local Script in 'PlayerGui'
You first want to find the Points Value
]]
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- Assuming the 'Points' value is in a folder named 'leaderstats'
local Points = player:WaitForChild("leaderstats").Points -- Gets the Value Object
local CurrentPoints = player:WaitForChild("leaderstats").Points.Value -- Gets the current number of the Points
--[[ So now we are going to check when the Value has changed,
and we will check how much has been added or taken away.
]]
Points:GetPropertyChangedSignal("Value"):Connect(function()
if Points.Value < CurrentPoints then -- Checks if the points it had before it changed is more than how many points there are now.
print((CurrentPoints - Points.Value)) -- Prints how many points have been taken away
elseif Points.Value > CurrentPoints then -- Checks if the points it had before it changed is less than how many points there are now.
print((Points.Value - CurrentPoints)) -- Prints how many points have been added
end
-- Now we want to update the 'CurrentPoints' variable if we want to make the same check again
CurrentPoints = Points.Value -- Updates the CurrentPoints variable
end)
Hopefully this is understandable.
Thank you very much! I have another question by the way. Is it possible to turn this into like a 2x multiplier script? Like let’s say the script detects that a value was changed for say I touched the block. Since the point giver is randomly chosen from 21-44 I don’t know how I will be able to multiply the amount if a person has the gamepass. Hopefully this makes sense I can give a much clearer explanation if you want me to.
It could be possible to turn this into a 2x multiplier script, but it can be a little complicated. What you can do, however, is you can multiply the random number by 2 (in the script that adds the value when a player touches the block), either if the player owns a gamepass, or if it is just like a limited time thing or something, up to you.
Example:
Block.Touched:Connect(function(hit)
-- lets just say you already got the player and the 'Points' value
local randomNumber = math.random(21,44)
Points.Value += (randomNumber * 2) -- multiplies the random number by 2
end)
Thank you! You are very helpful.
You are very welcome! Happy to help!
You should exclusively the ‘Changed’ event/signal when detecting changes in the values of value objects, its behavior works differently than that of the ‘Changed’ event/signal for other objects.
local function OnValueChanged(NewValue) --Connected functions receive the value object's new value whenever its 'Changed' signal/event is fired.
print(NewValue)
end
IntValue.Changed:Connect(OnValueChanged)
You should only use ‘GetPropertyChangedSignal()’ on value objects when listening for changes in their ‘Name’, ‘Parent’ properties etc.
Hello, I want to ask how to detect changed value in a property. I want to detect the orientation change in a vehicle seat. But it is under the property “CFrame”. I don’t want to detect when the position changes, I only want to detect the orientation change. How can I do this?
image|229x428