game.Players.PlayerRemoving:Connect(function(player)
im trying to get the player’s character when the player leaves,
but sadly this doesn’t work…
player.CharacterRemoving:Connect(function(char)
is there another way?
game.Players.PlayerRemoving:Connect(function(player)
im trying to get the player’s character when the player leaves,
but sadly this doesn’t work…
player.CharacterRemoving:Connect(function(char)
is there another way?
Try to get the players character when they got joined in.
Use it when the player left, storage the characters by copying to the serverstorage or dataservice etc. (Up to you)
why would you want to get the player’s character when he leaves?
ah I see, I’m basically trying to do some DataStores that make sure that if the player had an item equiped it will still be equiped when they join back so im trying to have that data saved somewhere yknow?
so the reason of why i need to save it before it leaves is because i need to check if the player had it equipped or not
you can make the player unequip all his tools before he leaves and then store all the items in his backpack
player.CharacterRemoving:Connect(function(char)
char.Humanoid:UnequipTools()
end)
this is already done, i have an inventory system done, however whatever is in the character doesnt get saved ( so im trying to save it and add it back when the player joins )
That’s not the same thing. Creating a system that when the player equip or unequipped the item. And when the player joins again, load the items from the datastore you ready up.
By the way, I don’t get your main goal. I’m writing this answer thinking that you are trying to do character items (In-game clothing etc.)
It would be nice if you try to explain your goals to help you.
sure thing!
dss = game:GetService('DataStoreService')
contributorDS = dss:GetDataStore('contributorDS')
player = game:GetService('Players')
rs = game:GetService('ReplicatedStorage')
game.Players.PlayerAdded:Connect(function(player)
local playerUserId = player.UserId
local data
local success, errorMessage = pcall(function()
data = contributorDS:GetAsync("UserInventory-"..playerUserId) --Load
end)
if success then
local folder = rs:FindFirstChild('Sample'):Clone()
local isSample = rs:FindFirstChild('ItemStorage')
folder.Name = 'Folder_'..playerUserId
folder.Parent = rs
if folder.Name == 'Folder_'..playerUserId then
for _,items in pairs(data) do
isSample[items]:Clone().Parent = folder
end
for _, child in pairs(folder:GetChildren()) do
child:Clone().Parent = player:WaitForChild('Inventory')
end
--task.wait(0.5)
-- print(player:WaitForChild('Inventory'):GetChildren())
else
print('Player_'..playerUserId..' had no items')
end
else
warn("Error: "..errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = player.UserId
local accessorySave = {}
local inv = player:FindFirstChild('Inventory')
for _,acc in pairs(inv:GetChildren()) do
table.insert(accessorySave, acc.Name)
end
local success, errorMessage = pcall(function()
contributorDS:SetAsync("UserInventory-"..player.UserId, accessorySave) --Save
-- print(accessorySave)
end)
end)
here’s the code i was using before, it basically makes sure the stuff in the player’s inventory loads properly ( this is already working perfectly )
however i also have a gui system
invEvent = game.ReplicatedStorage.InventoryEvent
game.Players.PlayerAdded:Connect(function(plr)
local inv = Instance.new('Folder')
inv.Name = "Inventory"
inv.Parent = plr
inv.ChildAdded:Connect(function(item)
invEvent:FireClient(plr, item.Name, true)
end)
invEvent.OnServerEvent:Connect(function(plr, ItemName, Value)
if Value == false then
local selItem = plr.Inventory:FindFirstChild(ItemName)
local char = plr.Character
local motor = Instance.new('Motor6D')
local item = selItem:Clone()
if char:FindFirstChild(ItemName) then
-- print(plr.Name..' is already wearing '.. ItemName)
char:FindFirstChild(selItem.Name):Destroy()
else
-- print(selItem)
-- print(char.Name)
item.Parent = char
motor.Parent = item
motor.Name = ItemName..'Motor6D'
motor.Part0 = char.Head
motor.Part1 = item.Handle
end
end
end)
end)
which makes sure the player can equip/unequip picked up items!
all of this works except that I haven’t made a way to check what items the player has added a hat to their character and then equip it again next time the player joins or spawns
EDIT: Changed the second code I had sent the wrong one
Sorry if i misunderstood, this hat is an In-game item, and the player can wear the hat whenever they want to. And your goal is that save up the information that is the player equipped the hat while he is playing the game? And load up when they rejoin?
yes its an in-game item that you can add to your inventory, I think this video can probably show everything it does so far
all of this works except when the player has it on the hat wont be already equipped when the player rejoins since I haven’t done any code to do that yet
Alright, i can give a recommendation about how can you finish that.
Firstly, you may give specific numbers to the hat or the in-game items that stored into a module script.
It would be like something like this
--Example Module Script
local myitems = script.Parent.Items
local list = {
"randomcode123" = myitems:WaitForChild("TheHat"),
}
--Example Datastore
local Itemlist = {
{itemcode = "randomcode123", isequipped = true,}
}
I would so something like that, it’s an example. I don’t have any idea about how dataservice works.
With that, if the itemcode in players database, you can give the hat to them. And if equipped true, you can make them equip that. Change the value of “isequipped” everytime the players equip or unequip that.
that sounds like it could work, i’ve never used module scripts so i might struggle a bit but i can try it
and having a “equipped” value sounds very smart
you can add an attribute for example called item into the accessory, and when the player leaves you check if the player has an accessory and that it has that attribute and then it stores it.
ex :
player.CharacterRemoving:Connect(function(char)
for i,v in pairs(char:GetChildren()) do
if v:IsA("Accessory") and v:GetAttribute("Item") then
-- stores it
end
end)
They are pretty easy. Remember them as they can return anything. You can get the returned item by using require(modulescript). They are very very useful.
you can get character by using .Character
property of player
local Players = game:GetService("Players")
Players.PlayerRemoving:Connect(function(Player)
local Character = Player.Character
-- your code here
end)
well characterremoving doesnt work when using playerremoving so i can’t really use it, which its why i came here
I’m pretty sure I tried that and it gave me a nil value back…
maybe I did it wrong??
This one should work i hope so
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterRemoving:Connect(function(char)
if game.ServerStorage.Characters:FindFirstChild(plr.Name) then
game.ServerStorage.Characters:FindFirstChild(plr.Name):Destroy()
end
char.Archivable = true
char:Clone().Parent = game.ServerStorage.Characters
end)
end)
Basically create folder named “Characters” in ServerStorage
Everytime player leaved or die character will be clone there
Combine with PlayerRemoving
give it some task.wait(1)
then it should be ok
you can’t assign the character after the player leaves cuz its gonna be removed already.
Player.Character
returns last spawned character and it cant be nil except for if the character didnt spawn even once