What did I do wrong

Hey everyone I’m currently working on a Tool ability System and rn I want that if the Tool is activated I can’t get damage now I tried it out and guess what it didn’t work btw. I got this from my Boi “Chat GPT”

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local shieldDuration = 5 -- Duration of the shield in seconds
local cooldownTime = 10 -- Cooldown time before the shield can be used again

local canUseShield = true
local shieldActive = false

local function activateShield()
	if canUseShield and not shieldActive then
		canUseShield = false
		shieldActive = true

		-- Shield logic: Reduce damage taken by half
		local function onDamageReceived(damage)
			if shieldActive then
				return damage * 0 -- Reduce damage by half
				return damage

			if humanoid.Health < humanoid.MaxHealth then
				humanoid.Health = humanoid.Health + (humanoid.MaxHealth - humanoid.Health) * 0.5

		-- Wait for the duration of the shield

		-- Deactivate shield
		shieldActive = false

		-- Start cooldown

		-- Allow the shield to be used again
		canUseShield = true

1 Like


never ever use chatgpt, not even as a basis. look at the code, and guide yourself with it, but don’t just copy it.


is there a way I can safe this script?

1 Like

well you could use a forcefield instead and you can make it invisible, and whenever you damage the player you need to use TakeDamage(number)


This code has many wrong things in it, for example the function onDamageReceived isn’t even used and wouldn’t work. I wouldn’t use chat gpts for many reasons. You should use a forcefield and make it invisible, as Brutus already said. Here you can read something about the forcefield: ForceField Documentation

Here’s a fixed version of your script (should be a Script parented to the tool):

local tool = script.Parent

local SHIELD_DUR: number = 5
local COOLDOWN: number = 10

local canUse: boolean = true

tool.Activated:Connect(function(): ()
    if not canUse then return end

    canUse = false

    local char = tool:FindFirstAncestorWhichIsA("Model")
    if not char then return end

    local hum = char:FindFirstChild("Humanoid")
    if not hum then return end

    local connection
    connection = hum.HealthChanged:Connect(function(): ()
        hum.Health = hum.MaxHealth

        hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false) -- Make sure Humanoid doesn't die anyways

    task.delay(SHIELD_DUR, function(): ()
    task.delay(COOLDOWN, function(): ()
        canUse = true

Or just use a Forcefield as others have suggested.

Explanation of everything wrong with the original script
  • This is written to be run on the client. This would do absolutely nothing on the client. (Players.LocalPlayer)
  • onDamageReceived() is never used or connected to anything
  • Unnecessarily has two booleans: canUseShield and shieldActive. Only canUseShield is required
  • What is this formula being used in the HealthChanged function? I can’t wrap my head around it. Probably doesn’t work.
  • Doesn’t correctly handle the character being changed (e.g. the character dies and respawns while the shield isn’t active. character and humanoid are now nil.)
1 Like

well it works but the duration of the shield is someway 2 seconds

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