Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
trapiz 4
5 years ago

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

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago

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.

0
I tried it, and there's no error, but it doesn't print the message that should print when the value changes. trapiz 4 — 5y
0
Change "EquippedWeapon = "Katana"" to "EquippedWeapon.Value = "Katana"" and see what happens BrockRocksYourSocks 48 — 5y
0
Yep, that's probably the issue. Happens to the best of us. Fifkee 2017 — 5y
0
The variable EquippedWeapon includes .Value at the end, so that can't be the problem. trapiz 4 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

0
That doesn't work either. trapiz 4 — 5y
0
Remove "Value" in "local EquippedWeapon = Player:WaitForChild("EquippedWeapon").Value" unpurrity 4 — 5y

Answer this question