Help with local script running 2 times

I have no idea why this code runs 2 times. Its a local script in startergui


local player = game.Players.LocalPlayer

while true do
	local energy = player:WaitForChild("Energy_Amount")
	if player:WaitForChild("Energy_Maker").Changed then
		print("1")
		if game.ReplicatedStorage.DoNotRemove.Value == player:WaitForChild("Energy_Maker").Value  then
			
		else
			print("2")
			if player:WaitForChild("Energy_Maker").Value == 1 then
				
				if energy.Value >= 3 then
				else
					wait(3)
					print(player:WaitForChild("Energy_Amount").Value)
					game.ReplicatedStorage["power sources"].oil["oil" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value
                     
				end

			end
			if player:WaitForChild("Energy_Maker").Value == 2 then
				if energy.Value >= 4 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"]["bio fuel"]["biofuel" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 3 then
				if energy.Value >= 10 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].tide["tideGen" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 4 then
				if energy.Value >= 5 then
				else
					wait(3)
					print("3")
					print(player:WaitForChild("Energy_Amount").Value .. script.Parent.Energy_Test.Value)
					game.ReplicatedStorage["power sources"]["natural gas"]["Natural Gas" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 5 then
				if energy.Value >= 5 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].GEOTHERMAL["biofuel" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 6 then
				if energy.Value >= 4 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].hydroelectricity["hydro" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 7 then
				if energy.Value >= 12 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].wind["windTurbine" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 8 then
				if energy.Value >= 4 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].coal["Coal Gen" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 9 then
				if energy.Value >= 8 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"].solar["solarPannel" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
			if player:WaitForChild("Energy_Maker").Value == 10 then
				if energy.Value >= 4 then
				else
					wait(3)
					game.ReplicatedStorage["power sources"]["nucular power"]["nucular" .. tostring(player:WaitForChild("Energy_Amount").Value)].Parent = game.Workspace
					game.ReplicatedStorage.DoNotRemove.Value = player:WaitForChild("Energy_Maker").Value

				end
			end
		end
		
	end


	wait(1)
end

1 Like

Your code seems a bit messy, what are you trying to?

I am moving a models from replicated storage to workspace when the Energy_Amount changes

Let me rephrase that.

What is your end goal?
We have to start from the beginning. :slight_smile:

I want to move these into workspace


local Descendants = game.ReplicatedStorage["power sources"]:GetDescendants();
for i = 1, #Descendants do
	local Object = Descendants[i];
	if Object:IsA("Model") then
		local Success, Error = pcall(function()
			local model = Object:Clone()
model.Parent = workspace
		end)

		if not Success then
			warn("An error occured while loading ")
		end		
	end
end

Not all at one time the player needs to buy a energy maker than the energy maker goes to workspace and they need to buy the correct one as well (Sorry if I confused you

You can use a .Changed event for that, and no you cannot use it in an if statement as it’s, well, an event not a property.

local player = game.Players.LocalPlayer
local energy = player:WaitForChild("Energy_Amount")
player:WaitForChild("Energy_Maker").Changed:Connect(function(NewValue)
   -- the rest of your code
end);

Its running 1 time but its not running when the number changed to 2 for amount of generators