Can CharacterAdded load once

can CharacterAdded load once like wanna load the item when the character load .
cuz its not working with
CharactedAdded

1 Like

do u mean CharacterLoaded or something then an item summons in?

1 Like

It fires once the character spawns, and fires again when the character respawns, if that’s what you’re asking.

1 Like

yes , i dont want it fire it again when i respawn

1 Like

Might not be possible, but correct me if I’m wrong.

1 Like

You can use this:

Player.CharacterAdded:Wait()
--Code
2 Likes

You could use what the post above says:
Player.CharacterAdded:Wait()

But do note that it yields the thread meaning any code past that won’t run until the character has loaded. A better solution I would use is:

local Connection; Connection = Player.CharacterAdded:Connect(function(Character)
— Your code here.
Connection:Disconnect()
end)

This creates a new connection and disconnects immediately after the event has been fired and the code above it has finished running. This runs only once.

8 Likes

Please could you be more specific about what you are asking and please could you clearly explain what you are trying to do. There could possibly be a better solution to do what you are trying to do.

As for what I am understanding about your question the CharacterAdded event fires every time the character object is added to the game. This means directly using the character added event wont be a viable solution. However the PlayerAdded event only fires when the player joins the game and not again through the play session. This means you could utilise this event to do what you are trying to do when the character first joins the game. See the code below to get a better idea of what I am talking about:

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.CharacterAdded:Wait() -- Waits for the character to be added and returns the character object
	
	-- Code: What you want to do when the character is first added
end)
1 Like

why local Connection;Connection

1 Like

Good question. If you mean why I’m not just doing local connection, it’s because the variable can’t be accessed since it’s in a different scope. If you mean why I am typing it on one line, it’s just a preference because why use two lines when one line works and it’s readable.

You could do:
local connection
connection =

It’s mostly just a preference.
The semi colon is used to separate the created variable and when I set the variable. The semi colon must be there if your using the one-line way.

1 Like

You don’t even need to do this, you can just in a script, have it do local character = player.Character or player.CharacterAdded:Wait() and then run the code once, without having to connect to the function then disconnect it.

1 Like

Like I said earlier, that isn’t recommended as it yields the current thread. Using an event also makes it way more flexible and you can pretty much use it anywhere without yielding the code. The question also wasn’t about getting the character.

1 Like

did i do anything wrong in here?

local Connection;function cloneBackpackToCharacter(Player)
	
	local equipped = Player.Inventory.EquippedBackpack

	local clone = game.ServerStorage:WaitForChild("Backpacks"):FindFirstChild(equipped.Value):Clone()
	clone.Name = "Backpack"
	clone.Parent = Player.Character
	
	Connection:Disconnect()
end
1 Like

A few things. First of all, I don’t see an event connection in there. Second, you are creating the variable but you aren’t setting it to the event connection when it’s created. Try this instead:

local Connection; Connection = Player.CharacterAdded:Connect(function(Character)	
 local equipped = Player.Inventory.EquippedBackpack

 local clone = game.ServerStorage:WaitForChild("Backpacks"):FindFirstChild(equipped.Value):Clone()
 clone.Name = "Backpack"
 clone.Parent = Character

 Connection:Disconnect()
end)

Just make sure that you have “Player” defined somewhere above this. Again sorry for the horrible format. (I’m on mobile).

2 Likes

oh cuz this is one of the function of this saving script

local Players  = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local EquippedData = DataStoreService:GetDataStore("EquippedData")
local BackpackData = DataStoreService:GetDataStore("BackpackData")

local Connection;function cloneBackpackToCharacter(Player)
	
	local equipped = Player.Inventory.EquippedBackpack

	local clone = game.ServerStorage:WaitForChild("Backpacks"):FindFirstChild(equipped.Value):Clone()
	clone.Name = "Backpack"
	clone.Parent = Player.Character
	
	
end

local function SaveBackpack(Player)
	
	local backpacks = Player.Inventory.OwnedBackpack:GetChildren()
	local BackpackTable = {}
	
	for _,v in pairs(backpacks) do
		table.insert(BackpackTable,v.Name)
	end

	local Success, Result = pcall(function()
		return BackpackData:SetAsync(Player.UserId,BackpackTable)
	end)
	
	if Success then
		print(Player.Name.."'s backpacks successfully saved!")
	else
		print("There was an error while saving "..Player.Name.."'s backpacks")
		error(Result)
	end
end

local function SaveEquipped(Player)
	
	local Equipped = {
		[1] = Player.Inventory.EquippedBackpack.Value
	}
	
	local Success, Result = pcall(function()
		return EquippedData:SetAsync(Player.UserId,Equipped)
	end)
	
	if Success then
		print("EquippedBackpack successfully saved!")
	else
		print("There was an error while getting "..Player.Name.."'s data")
		error(Result)
	end	
end

local function SaveInventory(Player)	
	SaveBackpack(Player)
	SaveEquipped(Player)	
end


local function LoadBackpack(Player)
	
	local Success, Result = pcall(function()
		return BackpackData:GetAsync(Player.UserId)
	end)
				
	if Success then
		if Result then
			for _,backpack in pairs(Result) do
				if game.ServerStorage:WaitForChild("Backpacks"):FindFirstChild(backpack) then
					local clone = game.ServerStorage:WaitForChild("Backpacks"):FindFirstChild(backpack):Clone()
					clone.Parent = Player.Inventory.OwnedBackpack
					print(backpack.." loaded into "..Player.Name.."'s inventory")
				else
					print("Backpack not found")
				end
			end
			print(Player.name.." has no backpacks")
		end
	else
		print("There was an error while getting "..Player.Name.."'s backpacks data")
		error(Result)
	end		
end

local function LoadEquipped(Player)

	local Success, Result = pcall(function()
		return EquippedData:GetAsync(Player.UserId)
	end)
	
	if Success then
		if Result then
			Player.Inventory.EquippedBackpack.Value = Result[1]
			print("Equipped backpack data loaded to "..Player.Name)	
			cloneBackpackToCharacter(Player)	
		else
			print(Player.Name.." has no equipped backpack")
		end
	else
		print("There was an error while getting "..Player.Name.."'s equipped backpack data")
		error(Result)
	end
	
end

local function CreateInventory(Player)
	
	local InventoryFolder = Instance.new("Folder")
	InventoryFolder.Name = "Inventory"
	
	local OwnedBackpack = Instance.new("Folder")
	OwnedBackpack.Name = "OwnedBackpack"
	OwnedBackpack.Parent = InventoryFolder
	
	local EquippedBackpack = Instance.new("StringValue")
	EquippedBackpack.Name = "EquippedBackpack"
	EquippedBackpack.Parent = InventoryFolder
	
	InventoryFolder.Parent = Player
	
	LoadBackpack(Player)
	LoadEquipped(Player)
	
end

Players.PlayerAdded:Connect(CreateInventory)
Players.PlayerRemoving:Connect(SaveInventory)
1 Like

Instead of passing an already created function to PlayerAdded, create a new function and call CreateInventory() inside of it and also use what I said above.

Something like this:
Players.PlayerAdded:Connect(function(Player) local connection; connection = Player.CharacterAdded:Connect(function(Character)
— Code above.
connection:Disconnect()
end)
CreateInventory()
end)

1 Like

but i need to check if player have equipped something or not
if yes then clone the item to the player so thats why i need to call a function in a function

2 Likes

so just like this?

local Connection; Connection = Player.CharacterAdded:Connect(function()
				cloneBackpackToCharacter(Player)
				Connection:Disconnect()
			end)	
1 Like

That works! 30 characters limit

2 Likes

thank you so much dude
.
.
.
.

2 Likes