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?
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!
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)
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.