I setup a simple characteradded event and its not working for some reasonn any reason why?
game.Players.PlayerAdded:Connect(function(player)
local char = player.Character
if char then
player.CharacterAdded:Connect(function(character)
print("Test")
end)
end
end)
This is exactly the issue. It isn’t getting to the event because that event fires when the character loads in. Remove that part (and the extra end), and you should be OK.
With PlayerAdded and CharacterAdded events it’s a good idea to account for the possibility that the player or character has already loaded before the event is set up.
local function onCharacterAdded(character)
print(character.Name.."'s character was spawned in.")
end
local function setUpPlayer(player)
-- Handle the character if it is already loaded when this function is called.
if (player.Character ~= nil) then
onCharacterAdded(player.Character)
end
-- Handle the character whenever it spawns in the future.
player.CharacterAdded:Connect(onCharacterAdded)
end
local players = game:GetService("Players")
-- Handle players who were already in the game when the script started running.
for _, player in ipairs(players:GetPlayers()) do
setUpPlayer(player)
end
-- Handle players when they join.
players.PlayerAdded:Connect(setUpPlayer)
You shouldn’t look for the character right away as their spawning in unless you put in a method to wait for it initially. It’s likely that it won’t find the character since it takes time to load in, thus your condition is never met, and the connection is never made. Make the function outside of the initial player connection, add a method to wait on the player’s character being added the first time, and then proceed to make the connection for future spawns as such:
function OnSpawn(Character)
--do stuff here
end
game.Players.PlayerAdded:Connect(function(Player)
OnSpawn(Player.Character or Player.CharacterAdded:Wait())
Player.CharacterAdded:Connect(OnSpawn)
end)
Using Player.Character or Player.CharacterAdded:Wait() will either run immediately if the player does have a character spawned already, or wait for one to be added if not.
And if for some reason in the future you need to have the Player variable present in the spawning function, you can change it like this:
function OnSpawn(Player,Character)
--do stuff here
end
game.Players.PlayerAdded:Connect(function(Player)
OnSpawn(Player,Player.Character or Player.CharacterAdded:Wait())
Player.CharacterAdded:Connect(function(char) OnSpawn(Player,char) end)
end)
Oof, its seems to be not working when i add a line to my code I dont get it @Blokav , when I add the userData part it doesnt let the character added fire or something
game.Players.PlayerAdded:Connect(function(player)
local function setupUserData()
local PlayerData = {
["Level"] = 1;
["EXP"] = 0;
["Max"] = 100;
["Double XP"] = false;
}
return PlayerData
end
local userData = DataStore2("key",player):Get(setupUserData())
player.CharacterAdded:Connect(function(character)
local LevelOver = game.ServerStorage.LevelOver:Clone()
local RankOver = game.ServerStorage.RankOver:Clone()
local NameOver = game.ServerStorage.NameOver:Clone()
LevelOver.Level.Text = "Level: "..userData["Level"]
LevelOver.Adornee = character:WaitForChild("Head")
LevelOver.Parent = character
RankOver.Rank.Text = "Rank: "..player:GetRoleInGroup(7231365)
RankOver.Adornee = character:WaitForChild("Head")
RankOver.Parent = character
NameOver.playerName.Text = "Name : "..player.Name
NameOver.Adornee = character:WaitForChild("Head")
NameOver.Parent = character
end)
end)
I don’t know how Datastore2 works. All my code does is make sure the function for the CharacterAdded event still fires if the player character already exists.
Hi! I think that I found a solution, I just wrote the CharacterAdded event before the data was loaded, for example:
game.Players.PlayerAdded:Connect(function(plr)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
Player.CharacterAdded:Connect(function(char)
-- The code of CharacterAdded is here
end)
-- Now I load the data here and write all the other code of PlayerAdded here
end)
I will to test it today, so, I’m not entirely sure it works, but I hope it does.
If char is a non false / nil value, then you won’t be connecting the CharacterAdded event. which is why it doesn’t fire.
This is the most reliable way, as the function CharacterAdded will be guaranteed to be called as long as the player’s character loads after some time.
local function CharacterAdded(character)
end
game.Players.PlayerAdded:Connect(function(player)
CharacterAdded(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(CharacterAdded)
end)