Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How should I go about making a kill counter?

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.

2 comments

1

Post an attempt mate

0

I did

1 answer

✓ Accepted answer

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

15 comments

0

There is a died event, whenever someone has died add a value onto the killcount

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.

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.

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.

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), ...

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.

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?

0

I cant check the value for me script

0

Reread the last paragraph -- you have to add the "Owner" ObjectValue yourself. I'll edit it an example.

1

A NPC can still have a humanoid, use :GetPlayerFromCharacter to see if it is a player.

0

@hiimgoodpack Thanks; I've updated the answer

0

Can you reread my post so you know that its updated and I need help

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.

0

Yea I did in my wand.

0

And I still doesn't work

This discussion is preserved as it was. New answers, comments, and votes are closed.