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

How should I go about making a kill counter?

Asked by 7 years ago
Edited 7 years ago

I'm trying to make a kill counter for my game WizardWars and so far I'm stuck and I don't know what to do I have LeaderStats and I clone my fireball.What I'm thinking is check if the ball is hit and hit.Parent:WaitForChild("Humanoid").Health and if the health = 0.But I'm stuck on how I can check the PlayerStats Because the Damage Script is in the Cloned FireBall.

So if anyone could help me that would be great!

Edit:It says you can't get owner is not a member of part here my update

wait(1)
local Kills = script.Parent.Owner.Value.leaderstats.Kills
local Dam = 10 -- Changre if want new damages
script.Parent.Touched:connect(function(hit)
    print(hit.Parent)
    local Human = hit.Parent:WaitForChild("Humanoid")
    if Human then
        print("Hit")
        Human:TakeDamage(10)
        if Human.Health <= 0 then
            Kills = Kills + 1
        end
        wait()
        script.Parent:Destroy()
    end
    wait(3)
    script.Parent:Destroy()
end)

This is in a local script in the fireball.

1
Post an attempt mate Goulstem 8144 — 7y
0
I did NerdBow_Gaming 41 — 7y

1 answer

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

The script doing damage needs to be given the player who cast it (otherwise how can it know who to attribute any kills to?). (By the way, don't use WaitForChild, since the thing the fireball hit might not ever get a Humanoid.) To amend your idea:

local kills = script.Parent.Owner.Value.leaderstats.Kills

script.Parent.Touched:Connect(function(hit)
    if not hit.Parent then return end
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if not hum or hum.Health <= 0 then return end -- not alive
    --if not game.Players:GetPlayerFromCharacter(hit.Parent) then return end -- uncomment this line if you only want to record kills on players rather than both players and NPCs
    hum:TakeDamage(damage)
    if hum.Health <= 0 then --killed something
        kills.Value = kills.Value + 1
    end
end)

In whatever script casts the fireball, it would need to create the Owner ObjectValue and set its value to the casting player before the fireball script runs (ex before the fireball is parented to the workspace). ex:

--Whereever it is that you clone the fireball, ex:
local fireball = game.ReplicatedStorage.Fireball:Clone()
--add something like this:
local owner = Instance.new("ObjectValue")
owner.Value = player -- 'player' must be defined elsewhere. If you do this in a RemoteEvent, then you should use that player. If this is in a LocalScript (which isn't compatible with FE but otherwise can work), then you could just use the LocalPlayer.
owner.Parent = fireball
-- You must parent the fireball to something already; be sure to do that last. ex:
fireball.Parent = workspace

0
There is a died event, whenever someone has died add a value onto the killcount Shadi432 69 — 7y
0
You can use the timeout parameter, and :WaitForChild would only wait in that event. It can still be called and other code can still execute. hiimgoodpack 2009 — 7y
1
That would be a KO counter Shadi. To count the number of kills someone achieves, you need to check when the player damages other players, and if it kills them, like in this answer. CootKitty 311 — 7y
0
However, this answer is flawed. You return from the function if their health is less than or equal to 0, and then check if their health is less than or equal to 0 before doing something. This could also error if the humanoid doesn't exist due to how you do your conditional statement. There's also no need to check if the parameter hit is nil or if it has a parent. It isn't going to be nil. CootKitty 311 — 7y
View all comments (11 more)
0
@hiimgoodpack: but there's no need to wait at all. If the Humanoid doesn't exist, it's not a player. @CootKitty: "if not hum or" means that the function will always return (without evaluating 'hum.Health') if 'hum' is nil due to short-circuit evaluation. You seem to have missed line 7, which potentially reduces the humanoid's health. You are right that "not hit" is pointless (I'll edit that), ... chess123mate 5873 — 7y
0
but it is possible that hit.Parent is nil -- ex, if a self-removing projectile hits script.Parent, then hit.Parent can be nil. chess123mate 5873 — 7y
0
Thank you for answering my question I will be sure to check this out and all so When you did Script.Parent.Owner How does that work? NerdBow_Gaming 41 — 7y
0
I cant check the value for me script NerdBow_Gaming 41 — 7y
0
Reread the last paragraph -- you have to add the "Owner" ObjectValue yourself. I'll edit it an example. chess123mate 5873 — 7y
1
A NPC can still have a humanoid, use :GetPlayerFromCharacter to see if it is a player. hiimgoodpack 2009 — 7y
0
@hiimgoodpack Thanks; I've updated the answer chess123mate 5873 — 7y
0
Can you reread my post so you know that its updated and I need help NerdBow_Gaming 41 — 7y
0
Where did you put the Owner value? The error is telling you that "Owner" doesn't exist. You have to create it and put it in the fireball, as I showed in the 2nd script. chess123mate 5873 — 7y
0
Yea I did in my wand. NerdBow_Gaming 41 — 7y
0
And I still doesn't work NerdBow_Gaming 41 — 7y
Ad

Answer this question