Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to save hats when player leaves in game?

`local DataStoreService = game:GetService("DataStoreService")
local Event = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("SaveMyHats")

game.Players.PlayerAdded:Connect(function(player)
	local Stats = Instance.new("Configuration", player)
	Stats.Name = "Hats"
end)

can't seem to figure out rest any guidance please? I want this data store to save when a player gets a hat and saves when it he/she rejoins. I believe their has to be a folder for it to save for this type of script.

Edit: formatted code

1 comment

0

You can't physically save a Hat instance in datastore. You would instead save a table of Hat names or unique hat IDs the player owns. When the player join again, you would load in their hats using the same table.

2 answers

✓ Accepted answer

There is an example on adding 'Hats' to leaderboard. This should work if your game is published to Roblox:

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
 
	local uniquekey = 'id-'..plr.userId
	local leaderstats = Instance.new('IntValue', plr)
	local savevalue =  Instance.new('IntValue')
	leaderstats.Name = 'leaderstats'
	savevalue.Parent = leaderstats
	savevalue.Name = 'Hats'
 
 
	local GetSaved = DSService:GetAsync(uniquekey)
	if GetSaved then
		savevalue.Value = GetSaved[1]
	else
	  	local NumbersForSaving = {savevalue.Value}
	  	DSService:SetAsync(uniquekey, NumbersForSaving)
 	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.leaderstats.Hats.Value}
	DSService:SetAsync(uniquekey, Savetable)
end)

Make sure this is a NORMAL script. You can put it to ServerScriptService or workspace EDIT: If you get an error like 403: Cannot write to DataStore from studio if API access is not enabled. just go to this website, find your game, click on the game's name (not at Start Place: ..., above), find Enable Studio Access to API Services, check that, click on save and you done!

5 comments

0

Then where do I put the hats where I want it to save?

0

Because I'm not looking for a leaderboard I'm looking for a script when a player receivves a hat it saves.

0

I think you can hide that if you want (because your hats' number will show up for every player), but if you do that what I wrote for your other comment, you can give any player any number of hats score, and it will save

0

yes this one is copied from alvinbloxx https://youtu.be/tOOS9lMIYes my bad

0

So incapaxx you still saying I copied from alvinbloxx?? Well, I learnt this DataSaving from him, but if someone ask where did I learnt that, I would say alvinbloxx. Also, why do I need to tell where did I read that? I bet there are many-many comments/answers on this forum where people copied from someone, and didn't told where from. I just helped the guy. Sorry if you think I copied from him..

Where do the hats go then?

2 comments

0

What hats? You have the 'hats' in the leaderboard. It's located at playername.leaderstats.Hats . You can change it's value from a script like game.Players.Miniller.leaderstats.Hats.Value = 5 this will give me 5 hats on the leaderboard

0

Dude, if you join the game, the 'Hats' as intValue will be located at game.Players.lavranzo.leaderstats.Hats . You can change it's value by changing game.Players.lavranzo.leaderstats.Hats.Value . This will save when player leave the game, and will give back the score when player rejoins

This discussion is preserved as it was. New answers, comments, and votes are closed.