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

Why My Leaderstats it's not working, I don't even get any error?

local DataStoreService	= game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local debounce = false
		game.Players.PlayerAdded:Connect(function(player)

		    local stats = Instance.new("StringValue", player)
		    stats.Name = "leaderstats"
			stats.Parent = player
		    
		    local Wins = Instance.new("IntValue")
		    Wins.Name = "Wins"
		    Wins.Value = 0
			Wins.Parent = stats
		
		local playerUserId = "Player_"..player.UserId
		
		myDataStore:GetAsync(playerUserId)
	
		-- LOAD DATA
		local data
		local success, errormessages = pcall(function()
			local data = myDataStore:GetAsync(playerUserId)
		end)
				
		if success then
			Wins.Value = data
			-- Set our Data
		end
		
		game.Players.PlayerRemoving:Connect(function(player)
			local playerUserId = "Player_"..player.UserId
			
			local data = player.leaderstats.Wins.Value
			
			local success, errormessages = pcall(function()
				myDataStore:SetAsync(playerUserId, data)	
			end)
			if success then
				print("Data Succesfully saved")
		else
				print("There was an error when saving Data")
				warn(errormessages)
		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
					-- 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)
		end)

Basically, I want when i Touch a brick, the stats to be saved, but it's not working

1 comment

0

I got another trick that works

1 answer

Make sure this is in a server script, but if it is then here is an another way you may have used a folder which works better with me and turn on api services on game settings for the data store to tell you whats wrong in the output, if you want the wins to save then use this script, i see how the value is always set to 0 which wont load it

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
local key = "Player_".. player.UserId
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = player

    local wins = Instance.new("IntValue")
   wins.Name = "Wins"
   wins.Parent = stats
   wins.Value = myDataStore:GetAsync(key) or 0
    
wins.Changed:Connect(function()
  myDataStore:SetAsync(key, wins.Value)
end)
end)

game.Players.PlayerRemoving:Connect(function()
  local key = "Player_".. player.UserId
 myDataStore:SetAsync(key, wins.Value)
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)	        
end)

this is all hand writen so it might not work if it does your welcome or if it doesnt then tell me whats wrong and ill fix it

2 comments

0

I'm getting errors at the 25) Line, about the workspace, I tried to change up the end at 23, and some other ends, but I can't find the right solution

0

Ill write it hand made in studio

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