Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

A block changes color when touched/Hit?

Local player = Game.Players.player
Local model = game.Workspace.Block
Local function steppedon ()

Player.humanoid = humanoid.player

Block.Brickcolor = Brickcolor.Green
Block.Material = Enum.Material.Grass

Any ideas?

2 answers

✓ Accepted answer
Local model = game.Workspace.Block

Block.Touched:Connect(function(p)
	if Game:GetService:("Players"):GetPlayerFromCharacter(p.Parent) then
		Block.Brickcolor = Brickcolor.Green
		Block.Material = Enum.Material.Grass
	end
end)

What happens is when something touches the Block then it will check if this "Thing" is actually a player, and if it is then it will turn the block Green and make the material grass.

2 comments

0

Thank you!!

0

Your Welcome!

A lot of the stuff you provide is not needed to make this script. Place this script inside of the block you want to change when hit, and mess with things as needed. I'll provide an explanation of what things do below the code block.

local subject = script.Parent
local debounce = true

function onTouched(hit)
	if debounce then
		debounce = false
		print("player touching")
		subject.BrickColor = BrickColor.Green()
		wait(2)
		subject.BrickColor = BrickColor.Blue()
		debounce = true
	end
end

script.Parent.Touched:connect(onTouched)

We define the script's parent, AKA the block affected by the script, above the function so the function can use the variable. We use a thing called "debounce" to prevent overflow, because the player can be touching the brick a whole lot -- this gives it a delay. We write the function out, print a string to let ourselves know the brick has been touched, and then change the parent's BrickColor. We connect the function with the Touched event that fires whenever anything is hit. Hope this helps, and happy coding!

1 comment

0

Ah, alright, thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.