I’m trying to insert the player’s character into a folder inside workspace upon the character being added, and remove the character upon the humanoid dying, but for some reason the CharacterAdded function doesn’t run at all
this is a server script located inside of StarterCharacterScripts
local plr = game:GetService("Players"):GetPlayerFromCharacter(script.Parent)
local char
local function RemoveFromFolder()
if workspace.Players:FindFirstChild(char.Name) then
workspace.Players[char.Name]:Destroy()
end
end
plr.CharacterAdded:Connect(function(model) -- nothing in here will run, not even a print
model.Parent = workspace.Players
char = model
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(RemoveFromFolder)
end)
Put your script in the server script service or workspace. When you put the script in startercharacter, the script is being replicated to the player’s character, a client structure, which is why the script won’t work
turns out roblox would instantly parent the character back to workspace upon me parenting it to the folder
not sure if this was the most optimal thing to do, but i just used runservice to change the parent
local plr
local char
local rService = game:GetService("RunService")
local function RemoveFromFolder()
if workspace.Players:FindFirstChild(char.Name) then
workspace.Players[char.Name].Parent = workspace
end
end
game:GetService("Players").PlayerAdded:Connect(function(player)
plr = player
plr.CharacterAdded:Connect(function(model)
rService.Heartbeat:Wait()
model.Parent = workspace.Players
char = model
print(char.Parent)
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(RemoveFromFolder)
end)
end)
local plr
local char
local rService = game:GetService("RunService")
local function RemoveFromFolder()
if workspace.Players:FindFirstChild(char.Name) then
workspace.Players[char.Name].Parent = workspace
char = nil
end
end
local function AddToFolder(model)
model.Parent = workspace.Players
char = model
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(RemoveFromFolder)
end
game:GetService("Players").PlayerAdded:Connect(function(player)
plr = player
if plr.Character then
AddToFolder(plr.Character)
end
plr.CharacterAdded:Connect(function(model)
rService.Heartbeat:Wait()
AddToFolder(model)
model.Parent = workspace.Players
end)
end)
Note - being less OOP is not a good way of coding, I followed your style for the script above.