function charspawn(player)
print(player.Name.." spawning")
end
function despawn(player)
print(player.Name.." despawning")
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(charspawn(player))
player.CharacterRemoving:Connect(despawn(player))
end)
help
when i join the game, it output prints out
RoyaleHigh_KingFB spawning
RoyaleHigh_KingFB despawning
why is despawning there when the character didnt get removed it just joined the game
and when i reset it doesnt print despawning or spawning
when i leave the game it also doesnt print anything
pls i dont understand this topic of players stuff how do i make it so every time i spawn or despawn it does something like printing
local Players = game:GetService("Players")
local function onCharacterSpawned(player)
print(player.Name .. " is spawning")
end
local function onCharacterDespawned(player)
print(player.Name .. " is despawning")
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function()
onCharacterSpawned(player)
end)
player.CharacterRemoving:Connect(function()
onCharacterDespawned(player)
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
The reason why your code isn’t running as expected is because your calling your respawn and despawn functions inside the connect function for CharacterAdded and CharacterRemoving instead of connecting them. This causes both those functions to run immediately.
The code below fixes the issue described above by connecting a function to both CharacterAdded and CharacterRemoving which then fires the corresponding onCharacterAdded or onCharacterRemoving functions passing the player and character as arguments.
local Players = game:GetService("Players")
local function onCharacterAdded(character: Model, player: Player)
print(player.Name .. "'s character has been added")
end
local function onCharacterRemoving(character: Model, player: Player)
print(player.Name .. "'s character has been removed")
end
local function onPlayerAdded(player: Player)
-- Connect CharacterAdded and then run onCharacterAdded function passing character and player as arguments
player.CharacterAdded:Connect(function(character: Model)
onCharacterAdded(character, player)
end)
-- Connect CharacterRemoving and then run onCharacterRemoving function passing character and player as arguments
player.CharacterRemoving:Connect(function(character: Model)
onCharacterRemoving(character, player)
end)
-- Run onCharacterAdded if the players character is already in the game
if player.Character ~= nil then
task.spawn(onCharacterAdded, player.Character, player)
end
end
-- Connect player added function
Players.PlayerAdded:Connect(onPlayerAdded)
-- Run onPlayerAdded for all the players currently in the game. This fixes cases
-- where PlayerAdded is connected after a player has joined the game
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAdded, player)
end
Side note:
This video below explains how to player added properly
In simple terms task.spawn or a coroutine allows multiple tasks to be ran at the same within the same script. For example, you could use task.spawn or a coroutine to run two while loops at the same time within the same script.
task.spawn spawns a new thread which runs immediately. Threads run alongside the main thread allowing multiple tasks to be ran at the same time. Think of threads like a multiple lanes on a road. Each lane represents a thread.