One of them is a server script inside ServerScriptService that clones the folder “PlayerInfoTemplate” from game.Players and renames it “PlayerInfo” and sets the folder’s parent to the local player. This one works fine. /
local Players = game.Players
local RunService = game:GetService("RunService")
local PlayerInfo = game.Players.PlayerInfoTemplate
local GaveTheInfo = false
RunService.Heartbeat:Connect(function(Replicate)
Players.PlayerAdded:Connect(function(plr)
if GaveTheInfo == false then
local Clone = PlayerInfo:Clone()
Clone.Parent = plr
Clone.Name = "PlayerInfo"
GaveTheInfo = true
end
end)
end)
This is the local script that’s supposed to read the values inside of PlayerInfo and determines if it should spawn the player’s character based on the values with a remote event. /
local RunSerivce = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local SpawnEvent = game.ReplicatedStorage.CharacterEvents.SpawnEvent
local Data
RunSerivce.Heartbeat:Connect(function(The)
Data = game.Players.LocalPlayer.PlayerInfo
if Data.CharacterStats.Modes.Spawned then
print("SPAWNED")
else
end
if Data.CharacterStats.Modes.Spawned.Value == false then
if Data.CharacterStats.Modes.Spawned.CanSpawn.Value == true then
if Data.CharacterStats.Modes.Spawned.Spawning.Value == false then
Data.CharacterStats.Modes.Spawned.Spawning.Value = true
end
end
else
end
if Data.CharacterStats.Modes.Spawned.Spawning.Value == true then
SpawnEvent:FireServer(Player, Data)
Data.CharacterStats.Modes.Spawned.CanSpawn.Value = false
Data.CharacterStats.Modes.Spawned.Spawning.Value = false
else
end
end)
How is that even possible? “Modes” came with the whole cloned folder and my local script is saying that there’s no such thing as that. To make this more confusing, the code directly under the RunService.Heartbeat prints “SPAWNED” if Data.CharacterStats.Modes.Spawned exists and it works fine. Something is broken and I don’t know what. This makes no sense and I have no clue how to fix it.
I think that’s because the script connection started while the folder is not loaded yet.
Maybey try to “WaitForChild” for the folder before the heartbeat connection so that when the event first executes, it will find the folder OR instead you can make an if statement and cancel the execution of the function if the folder isn’t found
Also, you don’t need to get the player every time here instead, use the Player variable which is at the top of the script while also defining it by GetService for safier execution (the same with replicated storage) so your local script may look like this with some good practices:
local RunSerivce = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local SpawnEvent = game:GetService("ReplicatedStorage"):WaitForChild("CharacterEvents"):WaitForChild("SpawnEvent")
--local Data -- This line is not needed anymore.
RunSerivce.Heartbeat:Connect(function(The)
local Data = Player:FindFirstChild("PlayerInfo")
local CharacterStats = Data:FindFirstChild("CharacterStats")
-- FindFirstChild is used so that it won't error if the folders aren't found.
if not Data or not CharacterStats then return end --| cancels the execution if the folders aren't found
if Data.CharacterStats.Modes.Spawned then
print("SPAWNED")
else
end
--[[if CharacterStats.Modes.Spawned.Value == false then
if CharacterStats.Modes.Spawned.CanSpawn.Value == true then
if CharacterStats.Modes.Spawned.Spawning.Value == false then
CharacterStats.Modes.Spawned.Spawning.Value = true
end
end
else
end ]] -- Shortened:
if CharacterStats.Modes.Spawned.Value == false
and CharacterStats.Modes.Spawned.CanSpawn.Value == true
and CharacterStats.Modes.Spawned.Spawning.Value == false then
CharacterStats.Modes.Spawned.Spawning.Value = true
end
if CharacterStats.Modes.Spawned.Spawning.Value == true then
SpawnEvent:FireServer(Player, Data)
CharacterStats.Modes.Spawned.CanSpawn.Value = false
CharacterStats.Modes.Spawned.Spawning.Value = false
else
end
end)
About the server-side script, you should not connect events like that!
You’re consuming the server’s resources with no valid purpose while you can just connect the PlayerAdded event Once and it will execute the function whenever a single player joins like that:
local Players = game:GetService("Players")
--local RunService = game:GetService("RunService") No need!
local PlayerInfo = Players.PlayerInfoTemplate
--local GaveTheInfo = false -- No purpose?
Players.PlayerAdded:Connect(function(plr)
local Clone = PlayerInfo:Clone()
Clone.Parent = plr
Clone.Name = "PlayerInfo"
-- GaveTheInfo = true
end)
Thank you! I went through with your tips and managed to work around the child issue. Also this sped up the speed of my game and got rid of some spawn glitches in the process.