Hello DevForum. Below is a day/night cycle script that changes the name of a zone by adding “Day” or “Night”. This is because I have a music script that matches the zone’s name with the music’s name. And since I want different music depending on whether it’s day or night, I need to change the name.
So the issue is that as the cycle script is right now, it will keep adding a day/night string onto the zone name which causes it to not match the music names at all. My question is what the best solution would be here.
I know it’s possible to change the music script by adding more if statmenets and relying on an attribute, however I figured this could be a good learning opportunity.
while true do
game.Lighting.ClockTime = 15
local zones = workspace.zones:GetChildren()
for i,v in pairs (zones) do
v.Name = (v.Name.."Day")
end
task.wait(10)
game.Lighting.ClockTime = 3
local zones = workspace.zones:GetChildren()
for i,v in pairs (zones) do
v.Name = (v.Name.."Night")
end
task.wait(10)
end
Create a base string, then concatenate the base string instead of modifying the already changed name. This will make sure it doesnt concat against itself.
local base_string = "topple"
v.Name = base_string .. "Day"
the base strings are all from a table, so I suppose you mean that I create a table with the names before they get changed to day? (not sure how it would look like in the script)
Btw I did just find a solution for it using the “gsub” function.
while true do
game.Lighting.ClockTime = 15
local zones = workspace.zones:GetChildren()
for i,v in pairs (zones) do
v.Name = (v.Name.."Day")
end
task.wait(10)
for i,v in pairs(zones) do
v.Name = string.gsub(v.Name, "Day", "")
end
game.Lighting.ClockTime = 3
local zones = workspace.zones:GetChildren()
for i,v in pairs (zones) do
v.Name = (v.Name.."Night")
end
task.wait(10)
for i,v in pairs(zones) do
v.Name = string.gsub(v.Name, "Night", "")
end
end
Using ‘gsub’ is a good method. Adding onto my method, since theres different zones you can store all the zone names in a table and use that as the base strings, and simply concat onto those using a quick index.
An example:
local zones = {}
local zonesPhysical = workspace.zones:GetChildren()
for Index, Zone in pairs(zonesPhysical) do
zones[Zone] = Zone.Name
end
while true do
game.Lighting.ClockTime = 15
for Index, Zone in pairs(zonesPhysical) do -- small mistake, mb (fixed)
Zone.Name = zones[Zone] .. "Day"
end
-- etc...
end