Sword Fighting Game Stats

Hello! I’m Tanvir known as ProjectTqnvirr, I’m trying to make a game called “Stay alive and make money” It’s like Stay alive and steal time but you make money instead of time, I just need help with the time overhead and a kill log, please help!

Pictures of how it should look:

You can make a value of the time:

Script in ServerScriptService

game.Players.PlayerAdded:Connect(function(player)

    local timeLabel = --Create the variable of the label up the head

    local timeAlive = Instance.new("IntValue", player)
    timeAlive.Value = 0
    timeAlive.Name = "TimeAlive"
    
    timeAlive.Changed:Connect(function()
        timeLabel.Text = player:FindFirstChild("TimeAlive").Value
    end)
end)

This will set the time in seconds.

You can also create a Script that when the match start does this:

repeat
    for _, players in pairs(game.Players:GetPlayers()) do
        players:WaitForChild("TimeAlive").Value = players:WaitForChild("TimeAlive").Value + 1
    end
    wait(1)
until --Set something that when reached stop the loop
1 Like

Small mistake, put IntValue in quotes like this
('IntValue', player)

1 Like

Sorry I forgot the them while writing…

1 Like

Time Overhead isn’t working

You need to create a BillboardGUI to insert in the Head of the players…

You can do it creating the BillboardGui with a TextLabel into it, put the BillboardGui in the ReplicaredStorage and do this in the previous Script.

game.Players.PlayerAdded:Connect(function(player)

    local timeLabel = --Create the variable of the label up the head

    local timeAlive = Instance.new("IntValue", player)
    timeAlive.Value = 0
    timeAlive.Name = "TimeAlive"
    
    timeAlive.Changed:Connect(function()
        timeLabel.Text = player:FindFirstChild("TimeAlive").Value
    end)

    local headGui = game.ReplicatedStorage:WaitForChild("NameOfTheBillboardGui"):Clone()
    headGui.Parent = player.Character:WaitForChild("Head")
end)

image

Put this in the ReplicatedStorage.

Uh, can you DM me on discord? ProjectTqnvirr#6765

and help me out in studio

I don’t have discord so I cannot do it.

Ah Okay

image

The DataStore In ServerScriptService code is

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```

and then the TimeScript code is

local humanoid = character.Humanoid

local player = game.Players:GetPlayerFromCharacter(character)

local leaderstats = player.leaderstats
local timeAlive = leaderstats["Time Alive"]
local bestTime = leaderstats["Best Time"]

local regionPart = workspace.Region

local min = regionPart.Position - (0.5 * regionPart.Size)
local max = regionPart.Position + (0.5 * regionPart.Size)
local region = Region3.new(min, max)

local function CheckSpawnRegion()
	local objects = workspace:FindPartsInRegion3WithWhiteList(region, {character})
	
	if #objects > 0 then
		if not character:FindFirstChildOfClass("ForceField") then
			local forceField = Instance.new("ForceField")
			forceField.Visible = true
			forceField.Parent = character
		end
		
		return true
	else
		local forceField = character:FindFirstChildOfClass("ForceField")
		
		if forceField then
			forceField:Destroy()
		end
		
		return false
	end
end

local hasDied = false

humanoid.Died:Connect(function()
	hasDied = true
end)

while wait() do
	if hasDied then break end
	
	while not hasDied and not CheckSpawnRegion() do
		timeAlive.Value += 1
		
		if timeAlive.Value > bestTime.Value then
			bestTime.Value = timeAlive.Value
		end
		
		wait(1)
	end
	
	if hasDied then
		timeAlive.Value = 0
	end
end```

What do you need now? I’m not understanding.

I need like the time overhead to match with the timescript and datastore scripts

but do you mind helping out in studio?

This is the group and I’ll rank you Velvet Productions - Roblox

So do you need a script that start the timer?

The OverHead Isn’t working

image

1 Like