Attempt to index local 'PlayerHasWeapon' (a boolean value)

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)
1 Like

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]]--
2 Likes

Try doing this:

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.

3 Likes

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
1 Like

I tried using GetPropertyChangedSignal before, It results in the same error.
[attempt to index local 'PlayerHasWeapon' (a boolean value)]

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()
2 Likes

Try the code that I have given you. It will probably work that way.

That code works perfectly but I rather not use while wait (0.1) because it’s also doesnt work instantly and may cause a bit lags…

1 Like

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 :slight_smile:

1 Like

brobrobroyo, i want to say that while wait() do will hurt performance, and is not recommended:

For OP: Try to instead of using a boolean value, use a number value, or an int value, if the value is higher than 1, your function will fire. :+1:

3 Likes

You are completely right. Thanks for the comment!

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:

PlayerHasWeapon.Value = true
1 Like