We use cookies on this site to enhance your user experience

Sep 07 2018, 2:35 AM PST

When placed inside a Player/Character model this code will give a player the ‘Midas touch’. Everything their character touches will change to gold.

When the Humanoid dies, this change is undone and the golden BasePart|BaseParts are returned to their original state.

To test this out, place this code inside a Script and place it in StarterCharacterScripts|StarterPlayer.StarterCharacterScripts.

local character = script.Parent 

local humanoid = character:WaitForChild("Humanoid")

local partInfo = {}

local debounce = false
local touchedConnection = humanoid.Touched:Connect(function(hit, limb)
	if debounce then
		return
	end

	-- make sure this part is valid 
	if not hit.CanCollide or hit.Transparency ~= 0 then
		return
	end

	-- make sure this part hasn't already been made gold
	if not partInfo[hit] then
		-- save info about this part for later
		partInfo[hit] = {
			BrickColor = hit.BrickColor,
			Material = hit.Material
		}

		-- make this part gold
		hit.BrickColor = BrickColor.new("Gold")
		hit.Material = Enum.Material.Ice

		-- apply debounce
		debounce = true
		wait(0.2)
		debounce = false
	end
end)

humanoid.Died:Connect(function()
	-- disconnect touched connection
	if touchedConnection then
		touchedConnection:Disconnect()
	end

	-- undo all of the gold
	for part, info in pairs(partInfo) do
		if part and part.Parent then
			part.BrickColor = info.BrickColor
			part.Material = info.Material
		end
	end
end)