Kill player take HALF the time they had

Hello developers! I am trying to make it so players gain time and when you kill the other player you take HALF of the time they had before. For example someone has 500 time and you kill them you get 250 if anyone can help me that would be awesome Thank you!

Script I am using

local ServerScriptService = game:GetService("ServerScriptService")
local DatStore2 = require(ServerScriptService.DataStore2)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			local timeStore = DatStore2("TimeAlive", player)
			timeStore:Set(0)
			local tag = character.Humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer then
				local timeStore = DatStore2("TimeAlive", killer)
				local killsStore = DatStore2("Kills", killer)
				timeStore:Increment(3) -- Take HALF of the time from player you killed
				killsStore:Increment(1)
			end
		end)
	end)
end)

you can try using Get()
example:

local halfTime = timestore:Get() / 2
--put this under the local timeStore = DatStore2("TimeAlive", killer) line ig they are values
local characterstimeStore = DatStore2("TimeAlive", character)
characterstimeStore.Value = characterstimeStore.Value + timeStore.Value/2
timeStore.Value = timeStore.Value/2

I hope this works

No sadly neither of these work if I use these it just resets the time to 1 which is odd because these are all IntValues meaning it should change.

I think this should work

local playerTimeStore = DataStore2("TimeAlive", player)
local Sum = playerTimeStore:Get() + timeStore:Get()
local halfTime = Sum / 2
timeStore:Set(halfTime)

whole script should look like this:

local ServerScriptService = game:GetService("ServerScriptService")
local DatStore2 = require(ServerScriptService.DataStore2)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			local timeStore = DatStore2("TimeAlive", player)
			timeStore:Set(0)
			local tag = character.Humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer then
				local timeStore = DatStore2("TimeAlive", killer)
				local killsStore = DatStore2("Kills", killer)
				local playerTimeStore = DataStore2("TimeAlive", player)
                                local Sum = playerTimeStore:Get() + timeStore:Get()
                                local halfTime = Sum / 2
                                timeStore:Set(halfTime)
				killsStore:Increment(1)
			end
		end)
	end)
end)
1 Like

Halfing a number will return decimals. For example, half of 3 is 1.5. Are you flooring the number to the next integer? This might be why it’ll save as 1.

I mean it somewhat works…It keeps putting it to decimal form on the leaderboard but it keeps taking away the Current amount the player has and just replacing it. eg: Player has 50 and he kills the other player that has 200 it just takes the 50 and replaces it with 2 instead of adding on half of their time to 50 if that makes sense

Oops well that makes sense then, I am trying to take half of the time they had and add it to the players current time that killed them

I see, yeah so you might have to think of ways to mitigate the decimals, maybe by just flooring it to the next value, or giving the players an even amount every kill, like 10.

Most of your scripting work has to do with just mathematics, but essentially, you are dividing the victim’s time by half, and then that half goes to the killer, which should be the same variable that you’re setting the victim’s time data.

I do recommend caching your player’s data into a folder with intvalues, so that you can visually see changes on the server and making sure each value is changing accordingly. This will also give you better optimization when saving and loading datastore values.

1 Like

Yeah everything is Cached into IntValues in a Stats folder I think it may be DataStore2 because even if i just “Add” an even number of 10 when I kill a player when I start with 0 time it just adds 10 instead. Kinda looks weird because it’s supposed to add onto it but instead it just gives 10 if that makes sense

I may switch to Profile service or even just a custom DataStore. Thank you for trying to help me tho!

1 Like

If you attempt to assign a number value which contains a decimal, i;e 0.5 to the “Value” property of an “IntValue” instance Roblox’s internal engine will automatically round the number to the nearest integer, which in the previous example would be 1.

I’d consider just using the regular DataStore service.