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

I fixed this script to make it deal damage but the cool down wont work? more in the description...

local canattack = true
if canattack == true then
    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(5)
            canattack = false
            wait(100)
            canattack = true
        end
    end)
end

The cool down wont work, as well as when i change the player's Max health + health to 500~~~~~~~~~~~~~~~~~ local player = game.Players.LocalPlayer local char = player.Character

char.Humanoid.MaxHealth = 500 char.Humanoid.Health = 500

after the humanoid got damaged by the part it will be lower then 100
Any clue?

2 answers

Put the function on line 2 and it will work.

local canattack = true script.Parent.Touched:Connect(function(hit) if canattack == true then if hit.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(5) canattack = false wait(5) canattack = true end end end)

No idea why but it works, this is what matters.

4 comments

0

Also about the thing with health, sry i have no idea.

0

I tried your way but the part wont damage the player, the output shows "FindFirstChild is not a valid member of the model"

0

Is your script inside of a part? I have tested the same script with part in worspace and this script inside of it and it worked.

0

its inside a mesh part

The reason your cooldown does not work is that by the time you change canattack to false, your TakeDamage function has already been executed.

local canattack = true

if canattack == true then
script.Parent.Touched:Connect(function(hit) 
	if canattack == true then
		canattack = false
		if hit.Parent:FindFirstChild("Humanoid")  then
			canattack = false -- now if players try to spam touch, they can't because canattack is set on false
			hit.Parent.Humanoid:TakeDamage(5)
			wait(100)
			canattack = true
		end
	end
end)
end

Hope this helps!

(Edit: Try this for your cooldown.)

local char = game.Players.LocalPlayer.Character

maxHealth = char:FindFirstChild("Humanoid").MaxHealth + 400

char.Humanoid.Health = maxHealth

3 comments

0

The script wont work sadly, and i wanna say that i can actually change the Max health/ health of the player, but when it got damaged by the part, the player will intake damage that will lower the health to <100 instead of taking 5 health only.

0

I'm editing this right now. You could try it again.

0

thanks that you fixed the script/cooldown, but the damage is fatal that it will lower player's health to <100 lel.

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