My code is now working fine, but Im woried. First thing is that I have a recursive function (a function that ends up calling itself) and i’ve read that this can lead to stack overflow (is it true in my case?). Second because of, for a weird motive that I have no idea what, my remote event only fires if I write the fire line 2 following times.
Actually, when the player enters it casts 2 times, but when it dies and reloads, all the following times are called once, even though there are 2 lines calling it. Odd! The function uses :LoadCharacter(), CharacterAutoLoads is off, maybe the cause is something related to that but i dont know.
This is the function, it is a function called by PlayerAdded:Connect on server. I dont know also why is that if I call this function inside PlayerAdded, a following CharacterAdded event does not fire at all, and obviously not a following Humanoid.Died event too. I wanted to make it to possibly prevent the stack overflow. Here is the full code:
local Charlist = {}
game.Players.CharacterAutoLoads = false
local function SpawnCharacter(Player)
local Character
local Humanoid
local EggsCount = 0
local testing = false
for i, v in pairs(workspace.GrownEggs:GetChildren()) do
EggsCount += 1
if v.Name == "Test" then
testing = true
end
end
if EggsCount > 0 and not testing then -- egg spawn
-- set starterChar Name
for i, v in pairs(game.StarterPlayer:GetChildren()) do
if v.Name == "StarterCharacter" then
v.Name = v.Role.Value
end
end
if not testing then
game.StarterPlayer.Baby.Name = "StarterCharacter"
else
game.StarterPlayer.Worker.Name = "StarterCharacter"
end
-- loads Char
Player:LoadCharacter()
Character = Player.Character
Humanoid = Character.Humanoid
-- destroy closer egg
local closer = {Part = {Position = Vector3.new(900,900,900)}}
for i, v in pairs(workspace.GrownEggs:GetChildren()) do
if (v.Part.Position - Character.HumanoidRootPart.Position).Magnitude < (closer.Part.Position - Character.HumanoidRootPart.Position).Magnitude then
closer = v
end
end
if closer.Part.Position ~= Vector3.new(900,900,900) then
closer:Destroy()
end
else -- no eggs spawn
-- set starterChar Name
for i, v in pairs(game.StarterPlayer:GetChildren()) do
if v.Name == "StarterCharacter" then
v.Name = v.Role.Value
end
end
if not testing then
game.StarterPlayer.Farmer.Name = "StarterCharacter"
else
game.StarterPlayer.Worker.Name = "StarterCharacter"
end
-- loads char in position
Player:LoadCharacter()
if not testing then
Player.Character:MoveTo(Vector3.new(math.random(-100,100), 10, math.random(-100,100)))
end
Character = Player.Character
Humanoid = Character.Humanoid
--=============================================================================
--Charadded
-- Variables
local Root = Character:WaitForChild("HumanoidRootPart")
-- LoadAppearence
repeat wait() until Player:HasAppearanceLoaded() and Player.Character.Parent == workspace
Charlist[Character] = Character
-- Hunger
local MaxHunger = 200
local Hunger = Instance.new("IntValue")
Hunger.Name = "HungerValue"
Hunger.Parent = Character
Hunger.Value = MaxHunger
spawn(function()
local connection = true
while connection do
wait(1)
if Hunger.Value > 0 then
Hunger.Value -= 1
end
if Hunger.Value < 1 then
Humanoid.Health -= 10
end
if Humanoid.Health == 0 or Humanoid:GetState() == Enum.HumanoidStateType.Dead then
connection = nil
end
end
end)
Hunger.Changed:connect(function()
if Hunger.Value < 0 then
Hunger = 0
end
if Hunger.Value > MaxHunger then
Hunger.Value = MaxHunger
end
end)
-- Grip Tool
local GripTool = Instance.new("Tool")
GripTool.Parent = Character
GripTool.Name = "GripTool"
GripTool.CanBeDropped = false
-- Turn Down Player Sound
RemoteEvent:FireAllClients("TurnDownPlayerSound", Character)
RemoteEvent:FireAllClients("TurnDownPlayerSound", Character)
-- Attention: Needs to be called 2 times, otherwise only will fire in first char life.
-- First life calls 2x, next lifes calls 1.
-- Sounds ----
for i, v in pairs(SoundService.ToRoot:GetChildren()) do
v:Clone().Parent = Root
end
-- adding things to char
local antflesh = game.ServerStorage.FleshEffect:Clone()
antflesh.Parent = Character.RightAnt2
local antflesh = game.ServerStorage.FleshEffect:Clone()
antflesh.Parent = Character.LeftAnt2
local GreenFeromon = game.ServerStorage.GreenFeromon:Clone()
GreenFeromon.Parent = Character.HumanoidRootPart
local RedFeromon = game.ServerStorage.RedFeromon:Clone()
RedFeromon.Parent = Character.HumanoidRootPart
local RLightParticle = game.ServerStorage.LightParticle:Clone()
RLightParticle.Parent = Character.RightAnt3
local LLightParticle = game.ServerStorage.LightParticle:Clone()
LLightParticle.Parent = Character.LeftAnt3
Humanoid.WalkSpeed = 13
--Leaderstats
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
-- Store plrs description
Appearences.Player = Character.Humanoid:GetAppliedDescription()
-- Health Destroy
if Character:FindFirstChild("Health") then
Character.Health:Destroy()
end
-- BreakJointsOnDeath
Humanoid.BreakJointsOnDeath = false
Humanoid.Died:Connect(function()
wait(5)
SpawnCharacter(Player)
end)
end
end
game.Players.PlayerAdded:Connect(SpawnCharacter)