Hi
I made a script that will regenerate the terrain if the nearest player is too far away.
script works flawlessly, but when the player falls out of the map or. his Character ceases to exist he writes me a error. (and the script stops working)
I gave: if Player.Character then … but it doesn’t work
here is an essential part of the code:
repeat
local closestchar = nil
for i, plr in pairs(game.Players:GetChildren()) do ---4
if plr.Character then --then player.character
local distance = (plr.Character.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude
if not closestchar or (closestchar.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude > range then
closestchar = plr.Character print(closestchar)
end
end
end ---4
if closestchar then --then closestchaaracter
local distance2 = (closestchar.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude
if distance2 > range then
-------- regen
print("regen")
script.Parent.Value.Value = false
else
print("he is close")
end
end
wait(cooldown)
until script.Parent.Value.Value == false
The code is not complete, but only in this part is there an error.
How else could the error be prevented?
or how would I make the script refresh?
I will be happy for any help.
local range = 75
local cooldown = 10
script.Parent.Touched:Connect(function(hit) —1
if hit.Parent:FindFirstChild(“Humanoid”) then —2
if script.Parent.Value.Value == false then —3
script.Parent.Value.Value = true
—loop
repeat
local closestchar = nil
for i, plr in pairs(game.Players:GetChildren()) do ---4
if plr.Character then
local distance = (plr.Character.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude
if not closestchar or (closestchar.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude > range then
closestchar = plr.Character print(closestchar)
end
end
end ---4
if closestchar then --ak charakter
local distance2 = (closestchar.HumanoidRootPart.Position - script.Parent.Parent.range.Position).Magnitude
if distance2 > range then
-------- regen
print("je ďaleko = regen")
game.Workspace.Terrain:FillBlock(script.Parent.Parent.Part1.CFrame, script.Parent.Parent.Part1.Size, Enum.Material.Basalt)
game.Workspace.Terrain:FillBlock(script.Parent.Parent.Part2.CFrame, script.Parent.Parent.Part2.Size, Enum.Material.Basalt)
script.Parent.Value.Value = false
print("Terén je obnovený a script je vypnutý")
else
print("je blízko")
end
end
wait(cooldown)
until script.Parent.Value.Value == false print("script off")
end ---3
end ---2
Player characters when falling in the void, doesn’t actually destroy the character. Rather it deletes all the parts (such as parts of accessories, root part, and body parts) but not instances such as the character model itself and the humanoid. It does this because the void can only detect BaseParts.
In this case, you may want to use FindFirstChild and check if the character has a HumanoidRootPart.
Here is a basic understanding of how it goes:
local character = player.Character
if character then
local HRP = character:FindFirstChild("HumanoidRootPart")
if HRP then
--if the instance exist or it is not nil, then run code
end
end
Can i might ask if there’s any chance (purely theoretically) that if script finds out there’s a HumanoidRootPart (or another part) and that’s when part is destroyed, he writes out an error in the next line when he needs that part? It didn’t happen to me, but it was possible? If so, it’s definitely too small, or is it impossible?
If you mean that if the part doesnt exist, yes you can use an else statement then use the warn() command to send a yellow colored text in the output.
local character = player.Character
if character then
local HRP = character:FindFirstChild("HumanoidRootPart")
if HRP then
--if the instance exist or it is not nil, then run code
else
--if the instance doesnt exist or it is nil, then run code
warn("warning")
end
end
Though if you meant to check if an already referenced part exists just got destroyed, im not so sure. There is a recent event of instance called Destroying, though on my end the event doesnt fire at all.
I dont think you should worry about this though, you may just do another FindFirstChild function then use the process like the code above.