i have a functional free punch script that adds a Kill stat when a humanoid’s health reaches 0, however it gives this stat to everyone. i’ve tried adding (player) before the expValue and after but i don’t get any results. ill add the relevant parts that are supposed to add the stat to the player who killed the other player/NPC. I’m just confused on what im supposed to do. I’m new to scripting
local vchar = tool.Parent
local PunchEvent : BindableEvent = game.ReplicatedStorage.punchdamage
local player = game.Players:GetPlayerFromCharacter(vchar)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local expValue = leaderstats:FindFirstChild("Kills")
end
expValue = 0
if action ~= "blocked" then
local dmg = math.random(10, 30)
if action == "uppercut" then
dmg = dmg + math.random(10, 25)
elseif action == "kick" then
dmg = dmg + math.random(25, 32)
elseif action == "dropkick" then
dmg = dmg + math.random(30, 60)
end
if humanoid.Health <= dmg then
humanoid.Health = 0
expValue = expValue + 1 -- Supposed to add a kill to the stat
end
humanoid.Health = humanoid.Health - dmg
PunchEvent:Fire(expValue) -- supposed to add the stat to the player
end"
if there’s any tips to improve this i’ll need it. Thank you! I can send the full function if requested.
I forgot to add the script that listens for the stat change and increment the player’s stat.
local DataStoreService = game:GetService(“DataStoreService”)
local KillsDataStore = DataStoreService:GetDataStore(“KillsDataStore”)
game.Players.PlayerAdded:Connect(function(player)
local function loadKills()
local success, result = pcall(function()
return KillsDataStore:GetAsync(tostring(player.UserId))
end)
if success and result then
return result
else
return 0
end
end
local function saveKills(amount)
local success, error = pcall(function()
KillsDataStore:SetAsync(tostring(player.UserId), amount)
end)
if not success then
warn("Failed to save kills for player " … player.Name … ": " … tostring(error))
end
end
local existingLeaderstats = player:FindFirstChild(“leaderstats”)
if existingLeaderstats then
local Punches = Instance.new(“NumberValue”)
Punches.Name = “Kills”
Punches.Value = loadKills()
Punches.Parent = existingLeaderstats
else
–something bad happened
end
if not existingLeaderstats then
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Punches = Instance.new(“NumberValue”)
Punches.Name = “Kills”
Punches.Value = loadKills()
Punches.Parent = leaderstats player:WaitForChild(“leaderstats”):WaitForChild(“Kills”).Changed:Connect(function()
saveKills(Punches.Value)
end)
end
end)
local PunchEvent : BindableEvent = game.ReplicatedStorage.punchdamage
game.Players.PlayerAdded:Connect(function(player)
local Punches = player:WaitForChild(“leaderstats”):WaitForChild(“Kills”, math.huge)
PunchEvent.Event:Connect(function(newPunches)
Punches.Value = Punches.Value + newPunches
end)
local function saveKills(amount)
local success, error = pcall(function()
KillsDataStore:SetAsync(tostring(player.UserId), amount)
end)
if not success then
warn("Failed to save kills for player " … player.Name … ": " … tostring(error))
end
end
end)