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

I'm trying to make the brick kill a player if it is a certain color?

Asked by
OhNope 6
6 years ago

I am new to scripting, so I am not too sure where I am going wrong. I have a separate script that changes the color of the brick every 1 second, and I want the brick to kill the player if the color lands on blue. This is the script I have:

local brick = script.Parent

function kill(part)
    if brick.BrickColor == Color3.new(0,0,1) then
        local h = part.Parent:FindFirstChild("Humanoid")
        if h then
            if h.Health > 0 then
                h.Health = 0
            end
        end
    end
end

script.Parent.Touched:Connect(function(kill)
end)

The script kills the player when I remove the color part, but I want it to kill the player ONLY if the brick is blue. Can someone tell me where I am going wrong, please?

3 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

First, you don't need function in connect if you already have a function. So:

script.Parent.Touched:Connect(kill)

Although I feel like you're over complicating things by changing humanoid health, an easier way would just do :BreakJoints()

local brick = script.Parent

function Kill(part)
if part  and brick.BrickColor == Color3.new(0,0,1) then
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then
plr.Character:BreakJoints()
else
print("Can't find plr/ Brick is not the right color")
end
end
end
end

brick.Touched:Connect(Kill)

Good luck!

0
Please properly indent your code. Also do not use deprecated methods like :connect. Also there is not a problem with setting the humanoid's health to 0. User#21908 42 — 6y
2
@Phlegethon, thanks for reminding me about that, also I didn't say there is a problem with setting humanoid health, I'm just saying I feel it would be easier to do BreakJoints titaneagle 0 — 6y
0
Thanks for the help, I now have my script working. I agree that BreakJoints is easier and I now use that! Thank you :) OhNope 6 — 6y
Ad
Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

BaseParts have two color properties: BrickColor and Color.

You can compare if BrickColor is a certain color by using BrickColor.new

Example:

local part = script.Parent

part.Touched:Connect(function(hit)
    if hit and hit:IsA("BasePart") then 
        local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
        if humanoid and hit.BrickColor == BrickColor.new("Bright red") then 
            humanoid.Parent:BreakJoints()
        end
    end
end)

Use Color3.new() or Color3.fromRGB() to compare the Color property.

Example:

local part = script.Parent

part.Touched:Connect(function(hit)
    if hit and hit:IsA("BasePart") then 
        local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
        if humanoid and hit.Color == Color3.fromRGB(255, 255 ,255) then 
            humanoid.Parent:BreakJoints()
        end
    end
end)
0
You don't need to check if hit is a BasePart... User#19524 175 — 6y
Log in to vote
0
Answered by
Stycon 30
6 years ago

You can also use values to set show what color the brick is. For example,

if brick.BrickColor == Color3
then script.Parent.StringValue.Value = 3

And set the kill based off of the StringValue.

if script.Parent.StringValue == "3"
then h.health = 0

Probably not correct scripting, but something along the lines of that.

Answer this question