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

Why won't .Changed for a string value work?

I want to make a local script run a function when the value of a string value called "EquippedWeapon" is changed. Here I have a local script which changes the string value upon a TextButton being pressed

local EquippedWeapon = game.Players.LocalPlayer:WaitForChild("EquippedWeapon").Value

Buttons = {
	script.Parent.Katana -- Katana is the TextButton
}



Buttons[1].MouseButton1Click:Connect(function()
	EquippedWeapon = "Katana"
	wait(1)
	print("EquippedWeapon")
end)

Now, here's a local script in StarterPlayerScripts which runs a function when EquippedWeapon's value changes.

local Player = game.Players.LocalPlayer
local EquippedWeapon = Player:WaitForChild("EquippedWeapon").Value

EquippedWeapon.Changed:Connect(function()
	print("Equipped Weapon value has been changed.")
end)

What should happen is that when EquippedWeapon's value is changed to "Katana", it should print a message. But the error message is this:
"Players.trapiz.PlayerScripts.GearChecker:4: attempt to index field 'Changed' (a nil value)"

Thanks :)

2 answers

Events only work on Instances, not strings/numbers/booleans. The Value of a StringValue returns a string, which does not have an event.

You should do Player:WaitForChild("EquippedWeapon") instead of Player:WaitForChild("EquippedWeapon").Value.

4 comments

0

I tried it, and there's no error, but it doesn't print the message that should print when the value changes.

0

Change "EquippedWeapon = "Katana"" to "EquippedWeapon.Value = "Katana"" and see what happens

0

Yep, that's probably the issue. Happens to the best of us.

0

The variable EquippedWeapon includes .Value at the end, so that can't be the problem.

Use EquippedWeapon.GetPropertyChangedSignal("Value"):Connect(function() instead of .Changed.

2 comments

0

That doesn't work either.

0

Remove "Value" in "local EquippedWeapon = Player:WaitForChild("EquippedWeapon").Value"

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