Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Soldier Name changer loop and Death Checking System?

I'm making a real time strategy game where units can be created/destroyed ect... I need help with a script I'm trying to make

Off=false

function Click1 ()

if workspace.Units.SoldierNumber.Value == 0 and Off == false then
	
local B = game.ReplicatedStorage.Grunt0
	
local clone = B:Clone()
	
clone:Clone().Parent = game.workspace.Units

workspace.Units.Grunt0:MakeJoints()

workspace.Units.SoldierNumber.Value = workspace.Units.SoldierNumber.Value + 1

--loop below to change names??
workspace.Units.Grunt0.Name = "Grunt1"


end
end

----------------------------------------------------------------------------------------------------------------------------------

--Remakes the troop if its dead

if workspace.Units.Grunt1.Alive.Value == false then do

local B = game.ReplicatedStorage.Grunt0
	
local clone = B:Clone()
	
clone:Clone().Parent = game.workspace.Units

workspace.Units.Grunt0:MakeJoints()

workspace.Units.SoldierNumber.Value = workspace.Units.SoldierNumber.Value + 1

workspace.Units.Grunt0.Name = "Grunt1"

print (GruntNumber[1])

--Creating = 2

end
end

I need this function to repeat but change the name of the troop in the process and check if one like it is alive already. I want it to spawn "Grunt(# here)" in then check if it is dead by using a Value( there is two inside the troop, Alive or Dead). If it is dead I want it remake the troop that is dead in order, then continue on making the next troop if the Grunt is still alive.

Example: If I spawn two troops in, Grunt1 and Grunt2 then later on Grunt1 dies I want Grunt1 to be built before Grunt3 is built.

Any help will be Appreciated! Thank You

1 answer

✓ Accepted answer

A Few Optimizations:

Use locals at the top of the code, globals can just make a bit more lag, and either way, in this context, locals would still work fine.

You may want to try using lowercase for your code, or abbreviations:

	local off = false
	local o = false
	
	-- etc. etc.

This just means your code is faster to write.

Try formatting your code with tab so you can distinguish different levels in your code:

	if true then
		if false then
			local function func()
				print("TAB IS USEFUL")
			end
		end
	end

Create variables for things you access often:

	local val = game.Workspace.Grunt1.Alive.

This is the finished code that I have come up with:

local off = false
local units = game.Workspace:WaitForChild("Units") -- gets the units folder
local s = units:WaitForChild("SoldierNumber") -- gets the soldier number value
local template = game.ReplicatedStorage:WaitForChild("Grunt0") -- gets the grunt to clone

function makeNew(num) -- num is used to respawn
	local g
	local val = 0
	if num then -- if the function is being called to respawn
		print("Respawning Grunt" ..num)
		g = template:Clone()
		g.Parent = units
		g:MakeJoints() -- you don't need to access the cloned grunt from scratch
		g.Name = "Grunt" ..num -- sets the name
		val = num -- sets the value
	elseif s.Value == 0 and off == false then
		print("Making new..")
		g = template:Clone()
		g.Parent = units
		g:MakeJoints() -- you don't need to access the cloned grunt from scratch
		s.Value = s.Value + 1
		g.Name = "Grunt" ..s.Value -- sets the name
		val = s.Value -- sets the value (for respawning)
	end
	if g then
		coroutine.resume(coroutine.create(function() -- to make the loop not stop code
			while wait() do --  repeats indefinately, so the grunts can die over and over
				repeat wait() until g.Alive.Value == false -- waits until the grunt is dead
				g:Destroy() -- deletes it
				makeNew(val) -- respawns
			end
		end))
	end
end

for i = 1,10 do -- makes 10 grunts over 10 seconds, for testing
	makeNew() -- doing makeNew(i) would work, but it the amount of soldiers wouldn't be added to
	wait(1)
end

Quick Crash Course in coroutines:

coroutines are basically things that allow you to run code while continuing onto other code, so that the code within is essentially being skipped, but also being ran at the same time.

Here's how to make one:

local routine = coroutine.create(function() end) -- coroutine.create's parameter needs to be a function
local routine2 = coroutine.create(makeNew) -- for example

To run the routine, you need to resume it:

coroutine.resume(routine)

To stop it,

coroutine.yield(routine) -- I'm not entirely sure if it's yield, just check the wiki post on coroutines

Hopefully I have given you all you need!

~TDP

4 comments

0

Wow! Thank you very much I'll try it and get back to you about how it is working

0

Cool! If you need to PM me, send a message to EmeraldSlash.

0

Alright I ran into a problem and its too long to post in this tiny comment box but Ill send you a PM in a day or two after I get back from vacation

0

Ok.

This discussion is preserved as it was. New answers, comments, and votes are closed.