I am wanting to change the color, material and transparency of an object over a 2 second period;
From: Red, Glass, Barely Visible
To: Yellow, Neon, Fully Visible
I can’t figure out how to do this, or find a forum that helps with this issue.
This is for a tower defense game, one of the tower will charge up and when Neon shoot.
This is the script I’ve written so far for the tower:
Summary
local tower = script.Parent.Position
local mobs = workspace.Board.MobRealm
local ball = tower.Parent.Ball
local function FindNearestTarget()
local maxDistance = 70
local nearestTarget = nil
for i, target in ipairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - tower.Position).Magnitude
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
end
return nearestTarget
end
while true do
local target = FindNearestTarget()
if target then
ball.Transparency = .8
ball.Material = "Glass"
ball.Color = Color3.new(255, 0, 0)
print(ball.Color)
target.Humanoid:TakeDamage(25)
end
task.wait(2.5)
ball.Transparency = .8
ball.Material = "Glass"
ball.Color = Color3.new(255, 0, 0)
print(ball.Color)
--2.5 between shots
end
I noticed you used Color3.new(255, 0, 0), this is RGB not Color3. try Color3.fromRGB(255, 0, 0) instead. also in your for loop, replace ipairs with pairs. ipairs is used for tables (i.e. {"value1", "value2", "value3"})
to change the color over 2 seconds, (assuming you want to animate it) you can use TweenService.
game.TweenService:Create(
ball,
TweenInfo.new(2),
{
Transparency = #,
Material = Enum.Material["#"],
Color = Color3.fromRGB(#, #, #)
}
):Play()
(obviously replacing the #s with your own values)
hope this helps
This is the updated targeting code with Tween included:
Summary
while true do
local target = FindNearestTarget()
if target then
print(ball.Color)
game.TweenService:Create(ball,TweenInfo.new(2),
{
Transparency = 1,
Color = Color3.fromRGB(255, 204, 0)
}
):Play()
print(ball.Color)
target.Humanoid:TakeDamage(25)
game.TweenService:Create(ball,TweenInfo.new(.5),
{
Transparency = .4,
Color = Color3.fromRGB(255, 0, 0)
}
):Play()
print(ball.Color)
end
task.wait(2.5)
--2.5 between shots
end
Whenever I run this now the color doesn’t change at all from it’s default color. The print functions all say the same:
All 3 Prints happen at the exact same time as well.
Could this have something to do with it being inside of a While true do loop?
while true do
local target = FindNearestTarget()
if target then
game.TweenService:Create(ball,TweenInfo.new(2),
{
Transparency = 0,
Color = Color3.fromRGB(255, 170, 0)
}
):Play()
wait(2)
target.Humanoid:TakeDamage(25)
game.TweenService:Create(ball,TweenInfo.new(.5),
{
Transparency = .4,
Color = Color3.fromRGB(255, 0, 0)
}
):Play()
wait(.5)
end
task.wait(2.5)
--2.5 between shots
end
I can help you shorten the code a bit too, here’s a more optimal version:
while task.wait(2.5) do
local target = FindNearestTarget()
if target then
game.TweenService:Create(ball, TweenInfo.new(2), { Transparency = 0, Color = Color3.fromRGB(255, 170, 0) }):Play()
task.wait(2)
target.Humanoid:TakeDamage(25)
game.TweenService:Create(ball, TweenInfo.new(.5), { Transparency = .4, Color = Color3.fromRGB(255, 0, 0) }):Play()
task.wait(0.5)
end
end