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

Can I have help awarding money to a player for every kill?

I need to figure out a way to award money to the player who kills one of the aliens in my game, I want to be able to award 25 for each kill, but I want ti to go the that one player, does anyone have any idea? If you have a place where I could go to, that would be great, if anyone could explain to me the basics of how I would do this, that would be greatly appreciated.

1 answer

This depends on how the weapons are scripted, but in most weapons, there is code to add a 'creator' tag to the Humanoid that the weapon is damaging. So to detect who killed what alien, you'd probably have something like this.

local Players = Game:GetService("Players")

function AlienKilled(alien, killer)
	print(killer.Name .. " killed an alien.")
	-- award points to `killer`
end

function AlienDied(alien)
	local tag = alien:FindFirstChild("Humanoid"):FindFirstChild("creator")
	if tag and tag.Value then
		AlienKilled(alien, tag.Value)
	end
end

function AlienSpawned(alien)
	alien:WaitForChild("Humanoid").Died:connect(function()
		AlienDied(alien)
	end)
end

Workspace.ChildAdded:connect(function(child)
	if child:IsA("Model") and child.Name:lower() == "alien" then
		AlienSpawned(child)
	end
end)

5 comments

0

I am pretty sure I mentioned I want it to only count when the player kills the alien.

0

By the looks of this script, it will only work when a player kills another player.

0

Ah, I misunderstood then. Basically the same concept though. I'll make an edit.

0

okay, thank you. This will be great, becuase I don't want players to have to buy money for upgrades... So would I put this in the weapon script?

0

I would put this in ServerScriptService.

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