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

I don't get any errors on DataStore, To Save The Stats, But It still doesn't save?

So Basically, when you touch the part, you get a "Win" but when you reconnect, it doesn't save.

local debounce = false
local dataStoreService = game:GetService("DataStoreService")
local statStore = dataStoreService:GetDataStore("StatStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local wins = Instance.new("IntValue",leaderstats)
	wins.Name = "wins"
	wins.Value = 0
	
	local recieved, notRecieved = pcall(function()
		stat = statStore:GetAsync("UserId-"..player.UserId)
	end)
	
	
	workspace.WinPart.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                if hit.Parent.Humanoid.Health > 0 then
                if debounce == false then
                    debounce = true
                    -- PLAYER TELEPORTS/wins
                         wins.Value = wins.Value + 1
                         hit.Parent.HumanoidRootPart.CFrame = CFrame.new(234, 45.906, -114)
                end
                else
                    print("You Lose")
               
                       
                end
        wait(1)
        debounce = false
            end
end)
	
	
	
	if recieved then
		wins.Value = stat
		print("Stat was recieved")
	else
		print("Stat was not recieved")
	end
	
	while wait(60) do 
    statStore:SetAsync("UserId-"..player.UserId,{wins.Value,})
    print("Stat was auto saved!")
end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player.leaderstats
	local wins = leaderstats.wins
	
	local saved, notSaved = pcall(function()
		statStore:SetAsync("UserId-"..player.UserId,{wins.Value})
	end)
	
	if saved then
		print("Stat was saved")
	else
		print("Stat was not saved")
	end
end)

1 comment

0

You are saving a table and attempting to read add 1 to a table.

1 answer

I think that line 57 statStore:SetAsync("UserId-"..player.UserId,{wins.Value}) Should look like this

statStore:SetAsync(player.UserId,wins.Value)

and also you should remove when its saving in line 46 cause i dont know if there any need for auto save

Basically your code should look like this

local debounce = false
local dataStoreService = game:GetService("DataStoreService")
local statStore = dataStoreService:GetDataStore("StatStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local wins = Instance.new("IntValue",leaderstats)
    wins.Name = "wins"
    local recieved, notRecieved = pcall(function()
        stat = statStore:GetAsync("UserId-"..player.UserId)
    end)
    if recieved then
        wins.Value = stat
        print("Stat was recieved")
    else
        print("Stat was not recieved")
    end
end)


workspace.WinPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Parent.Humanoid.Health > 0 then
			if debounce == false then
				debounce = true
				Game.Player:GetPlayerFromCharacter(hit.Parent).wins.Value = wins.Value + 1
				hit.Parent.HumanoidRootPart.CFrame = CFrame.new(234, 45.906, -114)
			end
		else
		print("You Lose")
end
wait(1)
debounce = false
end

end)
game.Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player.leaderstats
    local wins = leaderstats.wins

    local saved, notSaved = pcall(function()
        statStore:SetAsync(player.UserId, wins.Value)
    end)

    if saved then
        print("Stat was saved")
    else
        print("Stat was not saved")
    end
end)

2 comments

0

(27,77) Unknown global 'wins' I tried to switch up Ends a bit but still doesn't work

0

Also, I Tried to Replace only the line that you told me, but still doesn't work, can you add me Discord Aft3rL1fe#8610

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