Im making a bounty system for a game, right now im working on after the player has been killed 20 precent of the killed players bounty goes to the the player that killed him. Adding the Value works just fine but when subtracting the value from the player who died it adds a random amount then it subtracts back to what it orginally was making the value not go down at all. Its really werid heres my script for it.
local DataStoreService = game:GetService("DataStoreService")
local Players = game:getService("Players")
--//DataStore
local CurrencyData = DataStoreService:GetDataStore("BountyData")
--//Configuration
local CurrencyName = "Bounty"
local StartingValue = 10000
local playerkilledremote = game.ReplicatedStorage.Remotes.PlayerKilled
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Char)
local UserData
local success, errMsg = pcall(function()
UserData = CurrencyData:GetAsync(player.UserId)
end)
if success == false then
local doNotSave = Instance.new("Folder")
doNotSave.Name = "DoNotSave"
doNotSave.Parent = player
else
print("Data loaded!")
end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Bounty = Instance.new("IntValue")
Bounty.Name = CurrencyName
Bounty.Value = UserData or StartingValue
Bounty.Parent = leaderstats
Char:WaitForChild("Humanoid").Died:Connect(function()
for _, v in pairs(game.Workspace.Players:GetChildren()) do
local previousname = nil
if previousname == nil or previousname ~= v.Name then
previousname = v.Name
local otherplayer = game.Players:WaitForChild(v.Name)
local charbounty = player:WaitForChild("leaderstats").Bounty.Value
local PlayerData = _G["ASODUFON!)@$JV)S_DFN@)$!J@!#G}ASDFL#$%SAS_DF@?#$!@ - "..player.Name]
local OplayerData = _G["ASODUFON!)@$JV)S_DFN@)$!J@!#G}ASDFL#$%SAS_DF@?#$!@ - "..otherplayer.Name]
local bountyop = otherplayer.leaderstats.Bounty
if PlayerData.Level.Value - OplayerData.Level.Value >= 100 then return end
bountyop.Value = bountyop.Value + (charbounty * 0.2)
charbounty = charbounty - (charbounty * 0.2)
end
previousname = nil
end
end)
end)
end)
Players.PlayerRemoving:Connect(function(player)
local SavingPath = player.leaderstats:FindFirstChild(CurrencyName)
if player:FindFirstChild("DoNotSave") then
warn("Player data was not saved to avoid data loss.")
else
CurrencyData:SetAsync(player.UserId, SavingPath.Value)
end
end)
any help is appericated!!