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
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)
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)
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.
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)
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.
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)
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!