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