How do I prevent this NPC regen from freezing the game?
How can I prevent the game from freezing just before the NPC respawns?
Instead of it just cloning itself all the time and backing it up, would it be wise for it clone from lighting? Anyway here is the script that is inside the NPC
The top part of the code (function Respawn) is whats making NPC freezing the game before respawn.
Also Output says nothing about this
Thanks!
001 | local Model = script.Parent |
005 | local Backup = Model:clone() |
012 | script.Parent = Model.Parent |
014 | if (Model ~ = nil ) then |
020 | Model = Backup:clone() |
024 | local spawn = game:GetService( "Workspace" ):GetChildren() |
026 | if (Model.Configuration [ "Do not spawn on SpawnLocations?" ] .Value = = false ) then |
029 | if (spawn [ i ] .ClassName = = "SpawnLocation" ) then |
030 | team = game:GetService( "Teams" ):GetChildren() [ Model.Configuration [ "Attack specific team? (Read the help.)" ] .Value ] |
031 | if (team ~ = nil ) then |
032 | if (team.ClassName = = "Team" ) then |
033 | if (spawn [ i ] .TeamColor = = team.TeamColor) then |
035 | Model:MoveTo(spawn [ i ] .Position) |
037 | fft__ = spawn [ i ] .Duration |
041 | print ( "Invalid team specified." ) |
044 | if (Model.Configuration [ "Attack specific team? (Read the help.)" ] .Value ~ = 0 ) then |
045 | print ( "Non-existant team specified." ) |
047 | Model:MoveTo(spawn [ i ] .Position) |
049 | fft__ = spawn [ i ] .Duration |
051 | if (spawn [ i ] .Neutral = = true ) then |
053 | Model:MoveTo(spawn [ i ] .Position) |
055 | fft__ = spawn [ i ] .Duration |
062 | if (mvspawn = = false ) then |
064 | Model:MoveTo(Vector 3. new( 0 , 10 , 0 )) |
066 | fft__ = spawn [ i ] .Duration |
069 | Model.Parent = script.Parent |
075 | Model.Humanoid.Died:connect(Respawn) |
077 | function waitForChild(parent, childName) |
078 | local child = parent:findFirstChild(childName) |
087 | child = parent.ChildAdded:wait() |
089 | if child.Name = = childName then |
097 | local Figure = script.Parent |
098 | local Head = waitForChild(Figure, "Head" ) |
099 | local Humanoid = waitForChild(Figure, "Humanoid" ) |
101 | Figure.PrimaryPart = Head |
105 | function Joint(Name, Part 0 , Part 1 , C 0 , C 1 , MaxVelocity) |
106 | local Motor = Instance.new( "Motor" ) |
110 | Motor.MaxVelocity = MaxVelocity |
119 | local Torso = waitForChild(Figure, "Torso" ) |
120 | local LeftArm = waitForChild(Figure, "Left Arm" ) |
121 | local LeftLeg = waitForChild(Figure, "Left Leg" ) |
122 | local RightArm = waitForChild(Figure, "Right Arm" ) |
123 | local RightLeg = waitForChild(Figure, "Right Leg" ) |
126 | { "Right Shoulder" , Torso, RightArm, CFrame.new( 1 , 0.5 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , - 1 , - 0 , - 0 ), CFrame.new(- 0.5 , 0.5 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , - 1 , - 0 , - 0 ), 0.5 } , |
127 | { "Left Shoulder" , Torso, LeftArm, CFrame.new(- 1 , 0.5 , 0 , - 0 , - 0 , - 1 , 0 , 1 , 0 , 1 , 0 , 0 ), CFrame.new( 0.5 , 0.5 , 0 , - 0 , - 0 , - 1 , 0 , 1 , 0 , 1 , 0 , 0 ), 0.5 } , |
128 | { "Right Hip" , Torso, RightLeg, CFrame.new( 1 , - 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , - 1 , - 0 , - 0 ), CFrame.new( 0.5 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , - 1 , - 0 , - 0 ), 0.10000000149012 } , |
129 | { "Left Hip" , Torso, LeftLeg, CFrame.new(- 1 , - 1 , 0 , - 0 , - 0 , - 1 , 0 , 1 , 0 , 1 , 0 , 0 ), CFrame.new(- 0.5 , 1 , 0 , - 0 , - 0 , - 1 , 0 , 1 , 0 , 1 , 0 , 0 ), 0.10000000149012 } , |
130 | { "Neck" , Torso, Head, CFrame.new( 0 , 1 , 0 , - 1 , - 0 , - 0 , 0 , 0 , 1 , 0 , 1 , 0 ), CFrame.new( 0 , - 0.5 , 0 , - 1 , - 0 , - 0 , 0 , 0 , 1 , 0 , 1 , 0 ), 0.10000000149012 } } |
134 | for _, v in pairs (Joints) do |
138 | local RightShoulder = waitForChild(Torso, "Right Shoulder" ) |
139 | local LeftShoulder = waitForChild(Torso, "Left Shoulder" ) |
140 | local RightHip = waitForChild(Torso, "Right Hip" ) |
141 | local LeftHip = waitForChild(Torso, "Left Hip" ) |
142 | local Neck = waitForChild(Torso, "Neck" ) |
143 | local Humanoid = waitForChild(Figure, "Humanoid" ) |
144 | local pose = "Standing" |
146 | local toolAnim = "None" |
147 | local toolAnimTime = 0 |
151 | function onRunning(speed) |
171 | function onGettingUp() |
179 | function onFallingDown() |
187 | function onPlatformStanding() |
188 | pose = "PlatformStanding" |
192 | RightShoulder.MaxVelocity = 0.5 |
193 | LeftShoulder.MaxVelocity = 0.5 |
194 | RightShoulder.DesiredAngle = 3.14 |
195 | LeftShoulder.DesiredAngle = - 3.14 |
196 | RightHip.DesiredAngle = 0 |
197 | LeftHip.DesiredAngle = 0 |
203 | function moveFreeFall() |
204 | RightShoulder.MaxVelocity = 0.5 |
205 | LeftShoulder.MaxVelocity = 0.5 |
206 | RightShoulder.DesiredAngle = 3.14 |
207 | LeftShoulder.DesiredAngle = - 3.14 |
208 | RightHip.DesiredAngle = 0 |
209 | LeftHip.DesiredAngle = 0 |
213 | RightShoulder.MaxVelocity = 0.15 |
214 | LeftShoulder.MaxVelocity = 0.15 |
215 | RightShoulder.DesiredAngle = 3.14 / 2 |
216 | LeftShoulder.DesiredAngle = - 3.14 / 2 |
217 | RightHip.DesiredAngle = 3.14 / 2 |
218 | LeftHip.DesiredAngle = - 3.14 / 2 |
222 | for _, kid in ipairs (Figure:GetChildren()) do |
223 | if kid.className = = "Tool" then return kid end |
228 | function getToolAnim(tool) |
229 | for _, c in ipairs (tool:GetChildren()) do |
230 | if c.Name = = "toolanim" and c.className = = "StringValue" then |
237 | function animateTool() |
239 | if (toolAnim = = "None" ) then |
240 | RightShoulder.DesiredAngle = 1.57 |
244 | if (toolAnim = = "Slash" ) then |
245 | RightShoulder.MaxVelocity = 0.5 |
246 | RightShoulder.DesiredAngle = 0 |
250 | if (toolAnim = = "Lunge" ) then |
251 | RightShoulder.MaxVelocity = 0.5 |
252 | LeftShoulder.MaxVelocity = 0.5 |
253 | RightHip.MaxVelocity = 0.5 |
254 | LeftHip.MaxVelocity = 0.5 |
255 | RightShoulder.DesiredAngle = 1.57 |
256 | LeftShoulder.DesiredAngle = 1.0 |
257 | RightHip.DesiredAngle = 1.57 |
258 | LeftHip.DesiredAngle = 1.0 |
267 | if (pose = = "Jumping" ) then |
272 | if (pose = = "FreeFall" ) then |
277 | if (pose = = "Seated" ) then |
284 | if (pose = = "Running" ) then |
285 | RightShoulder.MaxVelocity = 0.15 |
286 | LeftShoulder.MaxVelocity = 0.15 |
289 | elseif (pose = = "Climbing" ) then |
290 | RightShoulder.MaxVelocity = 0.5 |
291 | LeftShoulder.MaxVelocity = 0.5 |
300 | desiredAngle = amplitude * math.sin(time*frequency) |
302 | RightShoulder.DesiredAngle = desiredAngle + climbFudge |
303 | LeftShoulder.DesiredAngle = desiredAngle - climbFudge |
304 | RightHip.DesiredAngle = -desiredAngle |
305 | LeftHip.DesiredAngle = -desiredAngle |
308 | local tool = getTool() |
312 | animStringValueObject = getToolAnim(tool) |
314 | if animStringValueObject then |
315 | toolAnim = animStringValueObject.Value |
317 | animStringValueObject.Parent = nil |
318 | toolAnimTime = time + . 3 |
321 | if time > toolAnimTime then |
338 | Humanoid.Died:connect(onDied) |
339 | Humanoid.Running:connect(onRunning) |
340 | Humanoid.Jumping:connect(onJumping) |
341 | Humanoid.Climbing:connect(onClimbing) |
342 | Humanoid.GettingUp:connect(onGettingUp) |
343 | Humanoid.FreeFalling:connect(onFreeFall) |
344 | Humanoid.FallingDown:connect(onFallingDown) |
345 | Humanoid.Seated:connect(onSeated) |
346 | Humanoid.PlatformStanding:connect(onPlatformStanding) |
350 | function waitForChild(parent, childName) |
351 | local child = parent:findFirstChild(childName) |
352 | if child then return child end |
354 | child = parent.ChildAdded:wait() |
355 | if child.Name = = childName then return child end |
360 | local sound = Instance.new( "Sound" ) |
362 | sound.archivable = false |
363 | sound.Parent = script.Parent.Head |
375 | sRunning.Looped = true |
379 | function onSoundDied() |
383 | function onState(state, sound) |
391 | function onSoundRunning(speed) |
401 | Humanoid.Died:connect(onSoundDied) |
402 | Humanoid.Running:connect(onSoundRunning) |
403 | Humanoid.Jumping:connect( function (state) onState(state, sJumping) end ) |
404 | Humanoid.GettingUp:connect( function (state) onState(state, sGettingUp) end ) |
405 | Humanoid.FreeFalling:connect( function (state) onState(state, sFreeFalling) end ) |
406 | Humanoid.FallingDown:connect( function (state) onState(state, sFallingDown) end ) |
408 | local runService = game:service( "RunService" ); |
411 | while Figure.Parent~ = nil do |
412 | local _, time = wait( 0.1 ) |
420 | local health = Humanoid.Health |
422 | if health > 0 and health < Humanoid.MaxHealth then |
423 | health = health + 0.01 * s * Humanoid.MaxHealth |
425 | if health * 1.05 < Humanoid.MaxHealth then |
426 | Humanoid.Health = health |
428 | Humanoid.Health = Humanoid.MaxHealth |