Need Help with an Damage/I-Frame script

I am once again looking to the Roblox Studio Forums for help. This time I made this script for a “kill brick” that will simply damage the player and then give them a tag that prevents them from taking damage for a short while. This is meant to replicate a health system similar to games like Mario where the health bar is segmented. I’ve tried looking online for resources or similar issues which led me to find out about tags, but the script is still buggy. As far as I know, the tags are given to the player but the block doesn’t do damage even if the player doesn’t have the tag. My script is below. I’ve labeled it with comments so you can get a feel for what I am trying to do

local damage = 20
local CS = game:GetService("CollectionService")
local tag = "iframes"

local function damage(otherPart) -- Checks if the object is a player
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChild("Humanoid")
	if humanoid then
		if not CS:HasTag(partParent, tag) then -- Checks if the player has the iframes tag. If it does not, the player takes damage 
			humanoid.Health = humanoid.Health - damage
			CS:AddTag(partParent, tag)
			wait(1) -- The player has iframes for 1 second after being damaged
			CS:RemoveTag(partParent, tag)
		end
	end
end

script.Parent.Touched:Connect(damage) -- The fuction is called on touch

If you need more information please let me know.

Your script works fine, you just happened to have named your function after one of your variables.

Note how you define the variable local damage = 20 and the function local function damage(otherPart). This causes your script to return an error when you try to subtract damage from humanoid.Health, because damage is both a number value and a function.

So, all you have to do is either rename the variable at the top or the function. :happy3:

1 Like

Wow the issue was a lot simple than I thought. Thanks! I feel
a little bad now because I made a whole topic for this :sweat_smile:

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