Not able to change attribute on player touch

Does anyone know why I cannot change an attribute when a player touches a part?

I am currently trying to make a game that lets the player switch between being a ghost and a person, and when the player spawns (touches the spawn point), the code below sets the attribute “ghost” to false.

function onTouch(part)
	humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Parent:SetAttribute("Ghost", false)
	end
end
script.Parent.Touched:connect(onTouch)

The killbrick only kills players with the “ghost” attribute set to false.
(Killbrick code below)

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

Lastly, this part should change a player’s “ghost” attribute to true when they touch it. However, it is not able to do so as the killbrick can still kill the player.
(Code below)

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)

I have checked that the killbrick works properly (setting “ghost” to true when player touches spawn makes the killbrick not do anything, so it is likely the last script that does not function.

Thank you for reading this! :slight_smile:

humanoid.Parent:SetAttribute("Ghost", true)

in the last script.

(Also, capitalize :Connect(), lower case is deprecated.)

@hfhfvvheh sorry for the typo.

Where would I put humanoid.Parent:SetAttribute?
Do I use it instead of ghost?

Thank you so much! This worked completely first try! :smiley:

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