Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would you track a player kill?

Asked by 6 years ago
Edited by OldPalHappy 6 years ago

In my game, I planned out that each time someone kills another person, they would gain a better weapon. I would like to understand the basics of tracking players and their kills.

0
First off, don't put your description of your problem in a code block. You use code blocks to show your scripts. BunnyFilms1 297 — 6y
0
He didn't. Tabbing your sentences does this by itself. OldPalHappy 1477 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You would have somewhere storing each players deaths and kills. I usually use a table in a server-side script somewhere. Something like:

local Stats = {}

Then when a player joins simply add another table that is the players name such as:

game.Players.PlayerAdded:Connect(function(Player)
    Stats[Player.Name] = {}
    Stats[Player.Name]['Kills'] = 0
    Stats[Player.Name]['Deaths'] = 0
end)

Same for when they leave:

game.Players.PlayerRemoving:Connect(function(Player)
    --Any save code here
    Stats[Player.Name] = nil
end)

Tracking player kills would go something like this. If you were going to have the weapon be a gun, each bullet should have a value of the person who shot it. Then if that player dies, grab the value and increment that players kills by 1. So a little bit of pseudo code would be like this:

local Event = someEventFiredWhenBulletSent

Event.OnServerEvent:Connect(function(Player, Bullet)
    --Draw bullet here, get humanoid, take damage, etc, see the attached link at bottom of answer
    if humanoid.Health <= 0 then
        --Give person shot a point towards death
        local Victim = game.Players:GetPlayerFromCharacter(humanoid.Parent).Name
        Stats[Victim]['Deaths'] = Stats[Victim]['Deaths'] + 1
        --Now give the shooter a point for killing
        Stats[Player.Name]['Kills'] = Stats[Player.Name]['Kills'] + 1
    end
end)

See: http://wiki.roblox.com/index.php?title=Making_a_ray-casting_laser_gun as a good example for drawing rays, getting the person who was shot humanoid.

0
Oh thanks, I get it now. InfernoExeuctioner 126 — 6y
Ad

Answer this question