Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

A block changes color when touched/Hit?

Asked by 5 years ago
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

Log in to vote
0
Answered by
Fad99 286 Moderation Voter
5 years ago
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.

0
Thank you!! fortesss7 40 — 5y
0
Your Welcome! Fad99 286 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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!

0
Ah, alright, thank you! fortesss7 40 — 5y

Answer this question