Steal Time | Help

I need you’re help to help me for a script like a steal time script

Ah, just simply get the Target Player’s Time value whenever they die by getting their creator tag

Probably something like this would do:

game.Players.PlayerAdded:Connect(function(Plr)
    local Time = Plr:WaitForChild("leaderstats").Time

    Plr.CharacterAdded:Connect(function(Chr)
        local Humanoid = Chr:WaitForChild("Humanoid")
        
        Humanoid.Died:Connect(function()
            local CreatorTag = Humanoid:FindFirstChild("creator")

            if CreatorTag then
               local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
               Time.Value += Leaderstats.Time.Value --This is stealing the target's time & adding more onto yours
               Leaderstats.Time.Value = 0
            end
        end)
    end)
end)

where do I put that script? serverscriptservice?

Pretty sure it’s a serverscript and it needs to be inserted into ServerScriptService.

Yeah ServerScriptService is fine, if you already have your Time leaderstats already made then you can just add that script as well

I tested it but it didn’t work

Can you check your Output for any errors? :thinking: You could just also that sample code inside where you first create your leaderstats, that’ll be easier to do

there’s no errors but should I give u the code of the leaderboard?

Yeah that’d help a lot :slightly_smiling_face:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

local function CreateStat(name, class, value, parent)
	local stat = Instance.new(class)
	stat.Name = name
	stat.Value = value
	stat.Parent = parent
	
	return stat
end

local function GetKey(player)
	return player.UserId .. "-"
end

local function SaveData(player)
	local key = GetKey(player)
	
	local leaderstats = player.leaderstats
	local timeAliveData = leaderstats["Time Alive"].Value
	local bestTimeData = leaderstats["Best Time"].Value
	
	local success, result = pcall(function()
		DataStore:SetAsync(key .. "TimeAlive", timeAliveData)
		DataStore:SetAsync(key .. "BestTime", bestTimeData)
	end)
	
	if not success then
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local key = GetKey(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local timeAlive = CreateStat("Time Alive", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("Best Time", "IntValue", 0, leaderstats)
	
	local timeAliveData, bestTimeData
	
	local success, result = pcall(function()
		timeAliveData = DataStore:GetAsync(key .. "TimeAlive")
		bestTimeData = DataStore:GetAsync(key .. "BestTime")
	end)
	
	if success then
		timeAlive.Value = timeAliveData or 0
		bestTime.Value = bestTimeData or 0
	else
		warn(result)
		player:Kick("Error occured while retrieving your saved data.")
	end
end)

game.Players.PlayerRemoving:Connect(SaveData)

if not RunService:IsStudio() then
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end
		
		for _, player in pairs(game.Players:GetPlayers()) do
			SaveData(player)
		end
	end)
end
game.Players.PlayerAdded:Connect(function(Plr)
    local Time = Plr:WaitForChild("leaderstats")['Time Alive']

    Plr.CharacterAdded:Connect(function(Chr)
        local Humanoid = Chr:WaitForChild("Humanoid")
        
        Humanoid.Died:Connect(function()
            local CreatorTag = Humanoid:FindFirstChild("creator")

            if CreatorTag then
               local Leaderstats = CreatorTag:FindFirstChild("leaderstats")
               
               if Leaderstats then -- check if leaderstats exists
               Leaderstats['Best Time'].Value += Leaderstats['Time Alive'].Value -- since we have a best time leaderstat
               Time.Value += Leaderstats['Time Alive'].Value
               Leaderstats['Time Alive'].Value = 0
               end
            end
        end)
    end)
end)

Could you add this when you first create your PlayerAdded event?

    player.CharacterAdded:Connect(function(Chr)
        local Humanoid = Chr:WaitForChild("Humanoid")
        print(player.Name.." has died!")

        Humanoid.Died:Connect(function()
            local CreatorTag = Humanoid:FindFirstChild("creator")

            if CreatorTag then
               local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
               ["Time Alive"].Value += Leaderstats["Time Alive"].Value --This is stealing the target's time & adding more onto yours
               ["Best Time"].Value = ["Time Alive"].Value
               Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

                print("Stole time!")
            else
                warn("Something else happened?")
            end
        end)
    end)

I tried but it didn’t workimage

is this good or no?

local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

local function CreateStat(name, class, value, parent)
	local stat = Instance.new(class)
	stat.Name = name
	stat.Value = value
	stat.Parent = parent
	
	return stat
end

local function GetKey(player)
	return player.UserId .. "-"
end

local function SaveData(player)
	local key = GetKey(player)
	
	local leaderstats = player.leaderstats
	local timeAliveData = leaderstats["Time Alive"].Value
	local bestTimeData = leaderstats["Best Time"].Value
	
	local success, result = pcall(function()
		DataStore:SetAsync(key .. "TimeAlive", timeAliveData)
		DataStore:SetAsync(key .. "BestTime", bestTimeData)
	end)
	
	if not success then
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Chr)
		local Humanoid = Chr:WaitForChild("Humanoid")
		print(player.Name.." has died!")

		Humanoid.Died:Connect(function()
			local CreatorTag = Humanoid:FindFirstChild("creator")

			if CreatorTag then
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				["Time Alive"].Value += Leaderstats["Time Alive"].Value --This is stealing the target's time & adding more onto yours
				["Best Time"].Value = ["Time Alive"].Value
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?")
			end
		end)
	end)
	local key = GetKey(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local timeAlive = CreateStat("Time Alive", "IntValue", 0, leaderstats)
	local bestTime = CreateStat("Best Time", "IntValue", 0, leaderstats)
	
	local timeAliveData, bestTimeData
	
	local success, result = pcall(function()
		timeAliveData = DataStore:GetAsync(key .. "TimeAlive")
		bestTimeData = DataStore:GetAsync(key .. "BestTime")
	end)
	
	if success then
		timeAlive.Value = timeAliveData or 0
		bestTime.Value = bestTimeData or 0
	else
		warn(result)
		player:Kick("Error occured while retrieving your saved data.")
	end
end)

game.Players.PlayerRemoving:Connect(SaveData)

if not RunService:IsStudio() then
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end
		
		for _, player in pairs(game.Players:GetPlayers()) do
			SaveData(player)
		end
	end)
end

Yeah, try that & see what print statements get outputted?

when I tested it, it didn’t work

Did anything get printed at all?

1 Like

I checked output and no errors

    print("Player Added")
	player.CharacterAdded:Connect(function(Chr)
        print("Character added")
		local Humanoid = Chr:WaitForChild("Humanoid")
		print(player.Name.." has died!")

		Humanoid.Died:Connect(function()
			local CreatorTag = Humanoid:FindFirstChild("creator")

			if CreatorTag then
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				timeAliveData += Leaderstats["Time Alive"].Value --This is stealing the target's time & adding more onto yours
				bestTimeData = timeAliveData
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?")
			end
		end)
	end)

Hm, try this?

Include the end)s and PlayerAdded listener!

game.Players.PlayerAdded:Connect(function(player)
    print("Player Added")
	player.CharacterAdded:Connect(function(Chr)
        print("Character added")
		local Humanoid = Chr:WaitForChild("Humanoid")
		print(player.Name.." has died!")

		Humanoid.Died:Connect(function()
			local CreatorTag = Humanoid:FindFirstChild("creator")

			if CreatorTag then
				local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
				timeAliveData += Leaderstats["Time Alive"].Value
				bestTimeData = timeAliveData
				Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0

				print("Stole time!")
			else
				warn("Something else happened?")
			end
		end)
    end)
end)