Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my tool not change a value within a part after colliding with it?

So basically i have an axe and a tree with a NumberValue that's name is "Health" and when I slash with the axe and the head of the axe collides with the tree its supposed to do 5 dmg but it doesn't even detect the number value

script:

Properties = {
	debounce = true, -- Leave this on true.
	sound_fx = true, -- If you don't want sound to play when tool is activated set this to false.
	anim_check = true, -- If you don't want animation to play when tool is activated set this to false.
	ToolDrop = false, -- Set this to true if you want to be able to drop the tool by pressing BackSpace.
	soundID = 6042531284, -- Sound Id.
	AnimID = 6461572129, -- Your Animtion id.
	debounce_lenght = 1, -- how long it will take before you can click again, aka cooldown so you can't spam click the tool.
	soundDelay = 0.25, -- How long to wait before playing sound
	enableTrail = true, -- Enable trail thing
	trail = script.Parent.Head.Trail, -- the trail thing
	damage = 5 -- how much dmg the thing does
}

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local BaseUrl = "rbxassetid://"
local anim = Tool:WaitForChild("Animation")
local Players = game:GetService("Players")


if Properties.sound_fx then	
	sound = Handle:WaitForChild("Sound")
	sound.SoundId = BaseUrl .. Properties.soundID
	sound.MaxDistance = 100
	sound.RollOffMode = Enum.RollOffMode.InverseTapered
end



if Properties.anim_check then	
	anim.AnimationId = BaseUrl .. Properties.AnimID
end


if Properties.ToolDrop == true then
	Tool.CanBeDropped = true
end

Tool.Equipped:Connect(function(Mouse) -- When you equip the tool
	local player = Players:GetPlayerFromCharacter(Tool.Parent) -- gets the player
	if Properties.anim_check == true then animation = player.Character.Humanoid:LoadAnimation(anim)end -- loads animation

	Tool.Activated:Connect(function() -- when you click with the tool
		if Properties.debounce then
			Properties.debounce = false
			if Properties.anim_check == true then animation:Play()end
			if Properties.sound_fx == true then wait(Properties.soundDelay) sound:Play()end

			if Properties.enableTrail == true then
				Properties.trail.Enabled = true
				wait(0.2)
				Properties.trail.Enabled = false
			end

			script.Parent.Head.Touched:Connect(function(Hit)
				print(Hit.Name)
				if Hit:FindFirstChild("NumberValue") and Hit:FindFirstChild("NumberValue").Name == "Health" then
					local temp = Hit:FindFirstChild("NumberValue")
					print(temp.Name .. ": " .. temp.Value)
					temp.Value = temp.Value - Properties.damage
				end
			end)

			wait(Properties.debounce_lenght)	
			Properties.debounce = true
		end	
	end)
end)
Tool.Unequipped:Connect(function()
	if Properties.anim_check == true then animation:Stop()end
	if Properties.sound_fx then sound:Stop()end
end)

3 comments

0

LocalScript or ServerScript?

0

Also is the name of the NumberValue “NumberValue”?

0

Server script and read the description

1 answer

✓ Accepted answer

I think the problem here is, you are looking for a child named “NumberValue” but not a NumberValue itself.

Use FindFirstChildWhichIsA() to find a type of child instead of the name of a child


Properties = {
    debounce = true, -- Leave this on true.
    sound_fx = true, -- If you don't want sound to play when tool is activated set this to false.
    anim_check = true, -- If you don't want animation to play when tool is activated set this to false.
    ToolDrop = false, -- Set this to true if you want to be able to drop the tool by pressing BackSpace.
    soundID = 6042531284, -- Sound Id.
    AnimID = 6461572129, -- Your Animtion id.
    debounce_lenght = 1, -- how long it will take before you can click again, aka cooldown so you can't spam click the tool.
    soundDelay = 0.25, -- How long to wait before playing sound
    enableTrail = true, -- Enable trail thing
    trail = script.Parent.Head.Trail, -- the trail thing
    damage = 5 -- how much dmg the thing does
}

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local BaseUrl = "rbxassetid://"
local anim = Tool:WaitForChild("Animation")
local Players = game:GetService("Players")


if Properties.sound_fx then 
    sound = Handle:WaitForChild("Sound")
    sound.SoundId = BaseUrl .. Properties.soundID
    sound.MaxDistance = 100
    sound.RollOffMode = Enum.RollOffMode.InverseTapered
end



if Properties.anim_check then   
    anim.AnimationId = BaseUrl .. Properties.AnimID
end


if Properties.ToolDrop == true then
    Tool.CanBeDropped = true
end

Tool.Equipped:Connect(function(Mouse) -- When you equip the tool
    local player = Players:GetPlayerFromCharacter(Tool.Parent) -- gets the player
    if Properties.anim_check == true then animation = player.Character.Humanoid:LoadAnimation(anim)end -- loads animation

    Tool.Activated:Connect(function() -- when you click with the tool
        if Properties.debounce then
            Properties.debounce = false
            if Properties.anim_check == true then animation:Play()end
            if Properties.sound_fx == true then wait(Properties.soundDelay) sound:Play()end

            if Properties.enableTrail == true then
                Properties.trail.Enabled = true
                wait(0.2)
                Properties.trail.Enabled = false
            end

            script.Parent.Head.Touched:Connect(function(Hit)
                print(Hit.Name)
                if Hit:FindFirstChildWhichIsA("NumberValue") and Hit:FindFirstChildWhichIsA("NumberValue").Name == "Health" then
                    local temp = Hit:FindFirstChildWhichIsA("NumberValue")
                    print(temp.Name .. ": " .. temp.Value)
                    temp.Value = temp.Value - Properties.damage
                end
            end)

            wait(Properties.debounce_lenght)    
            Properties.debounce = true
        end 
    end)
end)
Tool.Unequipped:Connect(function()
    if Properties.anim_check == true then animation:Stop()end
    if Properties.sound_fx then sound:Stop()end
end)

11 comments

0

Ty it works now, i just gotta add a de bounce system to it somehow,im not super familiar with how to make a debounce system so any help is appreciated

0

It looks like there is already a debounce implemented

0

there isnt?

0

Properties.debounce

0

wait nvm thats for the animation etc but i need to de bounce the damage etc

0

i added a simple debounce but it didnt seem to work for whatever reason

0

it does the damage 2 times

0

sometimes it does once, sometimes twice and sometimes 3 times

0

nvm i fixed the debounce somehow lol

0

nvm i fixed the debounce somehow lol

This discussion is preserved as it was. New answers, comments, and votes are closed.