How do you check if a variable inside a table has changed?

Hello developers! So I am making a chopping tree system, and I have created this table for each axe:

return {
	['Flimsy Axe'] = {Damage = 11; Health = 25; Can_Chop = {"TreeMesh"}};
	['Stone Axe'] = {Damage = 12; Health = 28; Can_Chop = {"TreeMesh"}};
	['Copper Axe'] = {Damage = 14; Health = 24; Can_Chop = {"TreeMesh"}, "Wood Planks"};
	['Iron Axe'] = {Damage = 13; Health = 30; Can_Chop = {"TreeMesh", "Wood Planks", "DIY Table"}};
	['Zinc Axe'] = {Damage = 13; Health = 32; Can_Chop = {"TreeMesh", "Wood Planks", "DIY Table"}};
}

However, in another script, I am getting the Damage, Health, and Can_Chop from the chosen axe. I tried to detect if the axe’s health was less than zero, but to no avail. Example of what I tried:

local function chop_tree(axe)
local axe_table = require(script.AXE_TABLE)
local health = axe_table[axe].Health
task.Wait(5)
health -= 100
if health <= 0 then
print("hi")
end
end

Any help will be appreciated :cowboy_hat_face:

1 Like

The only thing I know of is to use __newindex, I found an article which might just help you, it was solved:

1 Like

So, would I set the __newindex to a function? Or to like the array thats provided when you create a module script (the script that requires the table is a module script)

You’d set __newindex in the module script on the table

1 Like

This hasn’t quite worked for me yet… Maybe I’m doing it wrong?

Here is the chopping tree script:

local axe_table = require(script.AXE_TABLES)
local hit = {}
axe_table.__newindex = hit

function hit:chop_tree(instance, axe)
	--//Getting the axe variables
	local damage = axe_table[axe].Damage
	local can_chop = axe_table[axe].Can_Chop
	local health = axe_table[axe].Health
	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local instance_health = instance:WaitForChild("health_value")
	local debris = game:GetService("Debris")
	local gui = player.PlayerGui:WaitForChild("GUI"):WaitForChild("TEXT"):WaitForChild("BASIC_TEXT")
	local text = gui:FindFirstChild("TEXT")
	local name_gui = gui:FindFirstChild("NAME")
	local name = player.Name
	local seperator = ""
	local msg = "Oh noes! My "..axe.." Is broken.."
	local tween_service = game:GetService("TweenService")
	local TI = TweenInfo.new(0.355, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
	local TI_2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local goals = {
		BackgroundTransparency = 0;
	}
	local goal_2 = {
		BackgroundTransparency = 1;
	}
	--//Checking if the player is holding the axe
	if character:FindFirstChild(axe) and character[axe]:IsA("Tool") then
		--//Damaging the tree and the axe
		instance_health.Value -= damage
		health -= 17
		--//Detecting if the tree has died
		if instance_health.Value <= 0 then
			instance.Anchored = false
			instance.Position = instance.Position + Vector3.new(0,4,0)
			debris:AddItem(instance, 10)
		end
		--//Detecting if the axe has broke
		if health <= 0 then
			character:WaitForChild("Humanoid"):UnequipTools()
			player.Backpack:FindFirstChild(axe).Enabled = false
			player.Backpack:FindFirstChild(axe):Destroy()
			script.ANVIL_SHARP:Play()
			--//Playing a tween
			for _, ui in pairs(gui:GetChildren()) do
				tween_service:Create(ui, TI, goals):Play()
			end
			name_gui.Text = name
			for _, message in pairs(string.split(msg, seperator)) do
				text.Text = text.Text..message
				task.wait(0.05)
			end
			
			task.wait(2)
			
			for _, ui in pairs(gui:GetChildren()) do
				if ui:IsA("TextLabel") then
					ui.Text = ""
				end
			end
			for _, ui in pairs(gui:GetChildren()) do
				tween_service:Create(ui, TI_2, goal_2):Play()
			end
		end
	end
	
	return can_chop
end

Oh no you uhm… you move that all of inside a module script inside and you need to require the module script with a normal script. Also the table is not axe_table, it’s hit I am not sure what you are requiring in that script with axe_table. I recommend you to watch some tutorials how to use Module script (espacially OOP)

1 Like

Sorry for any confusion. I am requiring the axe table as it is what stores all of the axe’s properties/data. IE: Damage, Health, etc. I’ll try moving the axe table to the actual script itself.

It still doesn’t seem to be working… Maybe I will just set the health variable as a parameter for whenever I call the function.

I’d say something like this:
ModuleScript:

local axe_table = {
	['Flimsy Axe'] = {Damage = 11; Health = 25; Can_Chop = {"TreeMesh"}};
	['Stone Axe'] = {Damage = 12; Health = 28; Can_Chop = {"TreeMesh"}};
	['Copper Axe'] = {Damage = 14; Health = 24; Can_Chop = {"TreeMesh"}, "Wood Planks"};
	['Iron Axe'] = {Damage = 13; Health = 30; Can_Chop = {"TreeMesh", "Wood Planks", "DIY Table"}};
	['Zinc Axe'] = {Damage = 13; Health = 32; Can_Chop = {"TreeMesh", "Wood Planks", "DIY Table"}};
}
axe_table.__newindex = axe_table

function axe_table:chop_tree(instance, axe)
	--//Getting the axe variables
	local damage = axe_table[axe].Damage
	local can_chop = axe_table[axe].Can_Chop
	local health = axe_table[axe].Health
	local player = game.Players.LocalPlayer
	local character = player.Character or player.CharacterAdded:Wait()
	local instance_health = instance:WaitForChild("health_value")
	local debris = game:GetService("Debris")
	local gui = player.PlayerGui:WaitForChild("GUI"):WaitForChild("TEXT"):WaitForChild("BASIC_TEXT")
	local text = gui:FindFirstChild("TEXT")
	local name_gui = gui:FindFirstChild("NAME")
	local name = player.Name
	local seperator = ""
	local msg = "Oh noes! My "..axe.." Is broken.."
	local tween_service = game:GetService("TweenService")
	local TI = TweenInfo.new(0.355, Enum.EasingStyle.Back, Enum.EasingDirection.Out)
	local TI_2 = TweenInfo.new(0.6, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
	local goals = {
		BackgroundTransparency = 0;
	}
	local goal_2 = {
		BackgroundTransparency = 1;
	}
	--//Checking if the player is holding the axe
	if character:FindFirstChild(axe) and character[axe]:IsA("Tool") then
		--//Damaging the tree and the axe
		instance_health.Value -= damage
		health -= 17
		--//Detecting if the tree has died
		if instance_health.Value <= 0 then
			instance.Anchored = false
			instance.Position = instance.Position + Vector3.new(0,4,0)
			debris:AddItem(instance, 10)
		end
		--//Detecting if the axe has broke
		if health <= 0 then
			character:WaitForChild("Humanoid"):UnequipTools()
			player.Backpack:FindFirstChild(axe).Enabled = false
			player.Backpack:FindFirstChild(axe):Destroy()
			script.ANVIL_SHARP:Play()
			--//Playing a tween
			for _, ui in pairs(gui:GetChildren()) do
				tween_service:Create(ui, TI, goals):Play()
			end
			name_gui.Text = name
			for _, message in pairs(string.split(msg, seperator)) do
				text.Text = text.Text..message
				task.wait(0.05)
			end

			task.wait(2)

			for _, ui in pairs(gui:GetChildren()) do
				if ui:IsA("TextLabel") then
					ui.Text = ""
				end
			end
			for _, ui in pairs(gui:GetChildren()) do
				tween_service:Create(ui, TI_2, goal_2):Play()
			end
		end
	end

	return can_chop
end

return axe_table

Script:

local axe_Table = require(script.ModuleScript)

local instance = "whatevertheinstanceis"
local axe = "what everaxe is"

axe_Table:chop_tree(instance, axe)
1 Like

Okay, I’ll test it out. Thank you for helping me :cowboy_hat_face:

Also please remove the Solution from me, so that more experienced developers see on how to that :slight_smile:

1 Like

For some reason, its still not working. I did what you said, created the axe table inside the script and put all of the axes and their respective properties in there. Yet, it seems to still not be working. :slightly_frowning_face:

Here are screenshots of the script:


The second screenshot is where I get the axe table, and the first screenshot is where I call the chop tree function (the function that detects if the axes health is less than zero)

I am really sorry. I hope that others might correct the mistake that I’ve told you. Good Luck on your game tho’!

1 Like

Its alright! Thank you for stopping by to help me :smile:

1 Like

Had to use ChatGPT to help me, but I basically just stored the axe’s health in a table and created a variable for the axe’s previous health. I then just updated the axe’s current health every time the script was fired. After that, I used an if statement to detect if the axe’s current health was less than or equal to zero, and if the axe’s previous health was greater than zero. So glad this finally works! :cowboy_hat_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.