Why doesn't this script detect an attribute change?

I’ve been attempting to create a mining system, and so with that I need to ensure the ore can only be mined if there’s an active pickaxe tool. But, the problem here is that the localscript does not detect the change in attribute, regardless of what I do.

The problematic localscript

local AzureOre = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local OreTouchedEvent = replicatedStorage.OreTouchedEvent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0
local Active = false

Handle:GetAttributeChangedSignal("IsActivated"):Connect(function()
	if Handle:GetAttribute("IsActivated") == true then
		Active = true
	else
		Active = false
		
	end
end)

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Active == true then
			print("Active!")
		Count += 1
		local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
		if Cast then
			if Cast.Instance.Name ~= AzureOre.Name then return end
		else
			return
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(AzureOre)
	Count = 0
	end

The tool script that changes the attribute.

local Tool = script.Parent
local Handle = Tool.Handle

Tool.Activated:Connect(function()
	Handle:SetAttribute("IsActivated", true)
end)

Tool.Deactivated:Connect(function()
	Handle:SetAttribute("IsActivated", false)
	wait(3)
end)
2 Likes

I’m still looking for a possible answer to this!

So can you explain a little more. Is the problematic localscript in the workspace? Localscripts cant run in the workspace unless you use a feature using a Server Script where you set a scripts RunContext to Client

I did run the context of the script as client, so that isn’t the main issue I’m having.

Ok but the tool script that changes the attribute is also a localscript or a script? If its a regular script then the client wont be able to see any changes made to the IsActivated attribute

Oh! The tool script is but a regular script. I’ll try changing it to localscript, to see if this rectifies the issue.

You have it the wrong way around. The server cannot see changes the client has made.

Anyways, does your LocalScript even run at all? Any outputs? Add prints to see where it goes wrong.

The localscript that sends a raycast does function, and almost does what I want, but, it isn’t detecting whether the attribute has changed to “Activated” or “Deactivated”.

As shown here.
https://gyazo.com/cac42fa37b52ec27f71c970a7aa26bb2

Can you verify that the attribute is actually being changed on the server and you can see that in the Properties window on the server side?

Your client code also looks very much incomplete to me, can you send the entire script? You also say that your raycast works, indicating that it is passing your Active condition, which is only possible if it does detect the attribute being changed since your Active variable is initially false.

First of all you dont need to check if the attribute has changed. In the check function replace

If Active == true with if handle:GetAttribute("IsActivated") == true

Also your forgetting an end on the if statement if Active === true

you should do

If Active == true then
   print("Active!")
end

It is printing as it should thankfully!
image

Yes, does it work though or is there anything else happening?

I tried replacing “If Active == true” with “if Handle:GetAttribute(“IsActivated”) == true”, but despite this, it still isn’t firing the rest of the script, so it does nothing when I click using the placeholder pickaxe tool…

I tried putting a print statement, and the print statement doesn’t fire at the start of the script

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true then
			print("Active!")

Try moving this piece of code into the if Handle:GetAttribute("IsActivated") == true if statement.

Also make sure the tool script is a local script

Such as this?

Hitbox.Touched:Connect(function(player)
	local function Check()
		Count += 1
		if Handle:GetAttribute("IsActivated") == true then
		local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
		if Cast then
			if Cast.Instance.Name ~= AzureOre.Name then return end
		else
			return
		end
	end

	repeat task.wait(1) Check() until Count == 5
	OreTouchedEvent:FireServer(AzureOre)
	Count = 0
	end
end)

Also, the tool script is a local script, I’ve recently made that change.

Due to this change, the ore isn’t getting depleted…
image

Like this

if Handle:GetAttribute("IsActivated") == true then
	Count += 1
	local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
	if Cast then
		if Cast.Instance.Name ~= AzureOre.Name then return end
	else
		return
	end
end