I'm trying to make a script that prints the value of a StringValue that will change, but I'm to immature on Lua

I was debugging my script with a print function that will print the value of a certain StringValue, which will change randomly. But I’m not really sure how, so I will need some help on this, please provide some help with the current script I’m using.

local function StandPunched(player)	
	local StandNamea = player:WaitForChild("StandValue") ---Getting the StringValue from the Player
	StandNamea.Value.Changed:Connect() ---A really bad attempt at getting the latest value
	local StandName = StandNamea.Value ---StringValue's Value
	print(StandName) ---Printing Function
end

game.ReplicatedStorage.StandPunch.OnServerEvent:Connect(StandPunched)

I’ve tried the Dev Hub, but I didn’t find anything useful about this in it.
I tried to use Connect(Function() but It didn’t work out as much as I thought it would.

Thank you!

Should be StandNamea.Value.Changed:Connect(function()

Changed is not a valid a function of value. Instead of that, you can do

StandNamea.Changed:Connect(function(Prop) -- you can check what property changed

or

StandNamea:GetPropertyChangedSignal("Value"):Connect(function()

Actually the Changed event fires for a ValueBase object, not the value of one.

@R3tr0_Alit0

to detect when a value object’s value changes, do this

  StandNamea.Changed:Connect(function(v) -- assuming StandNamea is a string value
  print("new value = ", v)
  end)

Note that the Changed event fires for ValueBase objects only when the value changes, don’t use ::GetPropertyChangedSignal() for changes in value

1 Like
local function StandPunched(player)	
	local StandNamea = player:WaitForChild("StandValue")
	StandNamea.Changed:Connect(function(Value)
		print(Value)
	end)
end

game.ReplicatedStorage.StandPunch.OnServerEvent:Connect(StandPunched)

I’ve write it like this, but it’s still not printing the new value. StandNamea is a StringValue, by the way.
Any mistakes I’ve made?

Tried print(tostring(StandName))?
@R3tr0_Alit0

Didn’t work. I did the print(tostring(Value))

The parameter in the Changed function tells you what property has changed.

AnyVaueType.Changed:Connect(function(Property)
        if Property == "Value" then
             print(AnyValueType.Value)
       end
end)

You’re making a something wrong.
local StandNamea = player:WaitForChild("StandValue")
is going to be
local StandNamea = player:WaitForChild("StandValue").Value
You’re accessing the string property not the value of it.
@R3tr0_Alit0

Add Value to all the properties you want to have the value of.
Show me the output aswell.

This method did not work out for me, sorry.

But he wants to connect the StringValue object changed signal.

It’s giving me an error about "Attempting to index nil with “Connect” on StandNamea.Changed:Connect(function(Value)
after using the method you told me.

local function StandPunched(player)	
	local StandNamea = player:WaitForChild("StandValue").Value
	StandNamea.Changed:Connect(function(Value)
		if Value == "Value" then
			print(StandNamea.Value)
		end
	end)
end

game.ReplicatedStorage.StandPunch.OnServerEvent:Connect(StandPunched)

The whole script ^

image
^Output

Show me a screenshot of how you implement StandValue.

If you look at the API you need to call it like.

StandNamea.Changed:Connect(function(Value)
    print(Value)
end)

By the way, you only need to connect the StringObject once. So take it out of the event call.

I won’t be able to get the StringValue from the player if I take it out of the event call.

You should only connect it once then if its been connected. I’m on my phone so this is hard to type lol.

local playerConnections = {}

local function StandPunched(player)
        if not playerConnections[player] then
	    local StandNamea = player:WaitForChild("StandValue") ---Getting the StringValue from the Player
        playerConnections[player] = {}
        playerConnections[player].stringConnection = StandNamea.Changed:Connect(function(value)
    end) ---A really bad attempt at getting the latest value
	local StandName = StandNamea.Value ---StringValue's Value
	print(StandName) ---Printing Function
end

game.ReplicatedStorage.StandPunched.OnServerEvent:Connect(StandPunched)

Hope you kinda get it. Cant really type it all out I’m at work atm.

Should I add the changed StringValue to the parameter in line of script that fires the event?

Like,

local StandNamea = script.Parent.Parent:WaitForChild("StandValue")  ---The script is in the player's backpack and the Value is in player itself.
StandNamea.Changed:Connect(function(Value)
game.ReplicatedStorage.StandPunch:FireServer(Value)

I’m not very well with event callings and stuff like that, so correct me if I did something wrong up there.

game.ReplicatedStorage.StandPunch.OnServerEvent:Connect(function(plr)
      if plr:WaitForChild("StandValue").Changed then
             print(plr:WaitForChild("StandValue").Value)
       end)
end)

I don’t think it will work, but worth a try send me the output message.

It didn’t work.

The Value it was supposed to print was “StarPlatinum”.
Instead, it printed out the old value, which is “Fists”

Show me a full screen shot.

30 chars…