I am having issues with my script I made.
When I run it, I get this error: attempt to index local 'PlayerHasWeapon' (a boolean value)
local PlayerHasWeapon = false
local Equipped = true
PlayerHasWeapon.Changed:Connect(function(value) -- on this line
if PlayerHasWeapon == true and Equipped == true then
script.Parent.Text = "Equipped"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(100,81,173)
elseif PlayerHasWeapon == true and Equipped == false then
script.Parent.Text = "Equip"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(0,120,0)
end
end)
PlayerHasWeapon has no events. You cant put Changed there.
If its a BoolValue in the DataModel, you can get its changing signal with BoolValue:GetPropertyChangedSignal("Value"):Connect()
Another thing, if the value is indexed in the script, you should know when the value will change or not, since youre the one controlling the script. So with that you can do something like this
--changing the bool value
Equipped = true
--now you know the value changed because you did it
--[[code here]]--
local PlayerHasWeapon = false
local Equipped = true
while wait(0.1) do -- You can change "wait(0.1)" for the seconds you think necessary.
if PlayerHasWeapon == true and Equipped == true then
script.Parent.Text = "Equipped"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(100,81,173)
elseif PlayerHasWeapon == true and Equipped == false then
script.Parent.Text = "Equip"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(0,120,0)
end
end
You can also check this link for more information about Boolean Values.
You can use BoolValue (instance). It has the changed event, because it’s an instance, and some more stuff. Just insert it like any normal object (the plus menu when you hover an instance in explorer) into whatever you want. There are a few difficulties with them though, for example:
local wrongBool = workspace.BoolValue.Value
wrongBool = true --actual BoolValue won't change
local correctBool = workspace.BoolValue
correctBool.Value = true --this will actually set the BoolValue to true
Ah sorry, i didnt exactly tell you to use GetPropertyChangedSignal for PlayerHasWeapon.
PlayerHasWeapon is a pure bool value that is indexed in a script. It has no events, no descendants, no ascendants, its just a lone value with nothing to support it. The only thing you can do with it is change its values and check what value it is.
So when you change the value in the script, you can put your code in. Heres an example:
local function onupdate()
if PlayerHasWeapon and Equipped then
--your awesome code B)
end
end
--whenever you want to change the value
PlayerHasWeapon = false
onupdate()
Just do wait() without numbers. That should work very fast, and would not cause any problems.
I particularly use loops like this, on the client, and I don’t get problems.
But obviously, do it as you want
Instead of using a loop that goes on forever, you can do it using event-based programming by making a boolvalue object and giving it no parent. See below
local PlayerHasWeapon = Instance.new( 'BoolValue' )
local Equipped = true
PlayerHasWeapon.Changed:Connect(function(value)
if value == true then
if Equipped == true then
script.Parent.Text = "Equipped"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(100,81,173)
else
script.Parent.Text = "Equip"
script.Parent.image.BackgroundColor3 = Color3.fromRGB(0,120,0)
end
end
end)
When you want to change the value of PlayerHasWeapon you can do this instead: