Killbrick killing depending on variable

Hello, does anyone know how to make a killbrick which would only kill a player if a value in StarterPlayer is set to a certain string?

I am trying to make a game where the player can become a ghost or switch back, and they cannot be damaged when they are in the “ghost” form.

Currently, I have tried this code below but I don’t think that the script can find the StringValue I am telling it to find:

function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")
end
script.Parent.Touched:connect(onTouch)

if humanoid.CanBeDamaged == True and (humanoid ~= nil) then
	humanoid.Health = 0
end 

You can use “Attributes” in this case. First, when the player is a ghost, you set the attribute “Ghost” to true like so:

local character = game.Players.elomala.Character -- put whatever the character is here
character:SetAttribute("Ghost", true)

And then, when the player has to be damaged, you can check the attribute.

function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
       local Ghost = humanoid.Parent:GetAttribute("Ghost")
       if ghost then
            humanoid.Health = 0
       end
    end
end
script.Parent.Touched:connect(onTouch)

For more information, look at Attributes.

3 Likes

Do you know how I would use this for 2 parts? (One gives you the ghost attribute when you touch it and the other one can kill depending on if ghost is true or false)

for the part turns you into ghost you can do this

function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")
    if humanoid then
       humanoid.Parent:SetAttribute("Ghost", true) - - for example set to true
    end
end
script.Parent.Touched:connect(onTouch)

then the part that checks if ghost is true, you can just do what elomala did

Thank you so much! It works completely now! (I didn’t know that you could check one part’s attributes from another part)

1 Like

Could someone please check this code? This is code for a part that makes the player’s “ghost” attribute true if it is false.
However, I believe that the brick does not convert the player’s “ghost” to true, meaning that they can still be killed by killbricks.

I checked that the killbrick does not kill people who are ghosts, so the problem seems to be with the “ghost” attribute not being able to be set/not being found.

Thank you for reading this!


function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local ghost = humanoid.Parent:GetAttribute("Ghost")
		if ghost == false then
			ghost = true
		end
	end
end
script.Parent.Touched:connect(onTouch)

local ghost = humanoid.Parent:GetAttribute("Ghost")

this line gets the current value of the ghost attribute, not the attribute itself, so you when the variable changes, the attribute itself doesnt. if you want to set the value of the attribute, use the Instance:SetAttribute() function like @Lexiplaysroblox_Lexi has done.

Also, your original script can be fixed by changing this line:

if humanoid.CanBeDamaged == True and (humanoid ~= nil) then

to this:

if humanoid.CanBeDamaged.Value == "True" and (humanoid ~= nil) then

and moving the if statement inside of the onTouch function like this:

function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")

    if humanoid.CanBeDamaged == True and (humanoid ~= nil) then
        	humanoid.Health = 0
    end 

end

Your original script had three issues,

  1. You accessed the StringValue Instance instead of StringValue.Value, which is where the data is stored.
  2. You didn’t wrap True in quotes, which made the script think it was a variable and not a string.
  3. Your if statement would execute only once, and not when .Touched is fired.
    also, since the value is a either yes or no, i’d suggest using a BooleanValue, but it’d work with this as well

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