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

My script that is supposed to save the kills stats is not working. What did I do wrong?

local DSService = game:GetService('DataStoreService'):GetDataStore('Pizza')
game.Players.PlayerAdded:connect(function(Player)
	
	local uniquekey = 'id-'..player.userId
	local leaderstats = Instance.new('IntValue',player)
	local savevalue = Instance.new('IntValue')
	leaderstats.Name = 'leaderstats'
	savevalue.Parent = leaderstats
	savevalue.Name = Kills
	
	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(player)
	local uniquekey = 'id-'..player.userId
	local Savetable = {player.leaderstats.Kills.Value}
	DSService:SetAsync(uniquekey,Savetable)
	
end)

I made a script that is supposed to save your kills after you leave the game. This is what the script looks like:

What is wrong? If its something simple please change it and comment it. Thanks.

4 comments

0

Looks like the script came in a bit buggy, hopefully its understandable

0

edit it and try to redo it

0

also savevalue.Name = 'Kills' not kills

0

Put the script is a code block by pressing the Lua symbol in the edit page and placing the code between the two sets of tildes.

1 answer

I think the problem is in line 9. The name of "savevalue" was supposed to be a string (word in quotation marks), and you didn't add them for the name. Apart from that, you should now use "UserId" instead of "userId" which was deprecated.

Here is the script:

local DSService = game:GetService('DataStoreService'):GetDataStore('Pizza')
game.Players.PlayerAdded:connect(function(Player)

    local uniquekey = 'id-'..player.UserId -- "userId" is deprecated, use "UserId" instead!
    local leaderstats = Instance.new('IntValue',player)
    local savevalue = Instance.new('IntValue')
    leaderstats.Name = 'leaderstats'
    savevalue.Parent = leaderstats
    savevalue.Name = "Kills" -- Here! You should use ""s for names.

    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(player)
    local uniquekey = 'id-'..player.UserId --"userId" is deprecated, use "UserId" instead!
    local Savetable = {player.leaderstats.Kills.Value}
    DSService:SetAsync(uniquekey,Savetable)

end)

If you have any problems, please leave a comment below. Thank you, and I hope this will help.

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