How to make a Kill to earn money system?

Hello There! I am making a game where you beat enemies for money. I have made the database system, but I can’t make the part where you earn money after beating it. its something related to the creator tag, all I know.

this is the script for data store if you need:

local datastore = game:GetService("DataStoreService"):GetDataStore("leaderstats")

game.Players.PlayerAdded:Connect(function(plr)

	wait()

	local plrkey = "id_"..plr.userId

	local savevalue = plr.data.Cash

	local GetSaved = datastore:GetAsync(plrkey)

	if GetSaved then

		savevalue.Value = GetSaved[1]

	else

		local NumbersForSaving = {savevalue.Value}

		datastore:GetAsync(plrkey, NumbersForSaving)

	end

end)

game.Players.PlayerRemoving:Connect(function(plr)

	datastore:SetAsync("id_"..plr.userId, {plr.data.Cash.Value})

end)

and the second data store script:

game.Players.PlayerAdded:Connect(function(plr)

	local data = Instance.new("Folder", plr)
	data.Name = "data"

	local Cash = Instance.new("IntValue", data) -- Create a score variable
	Cash.Name = "Cash" -- Name
	Cash.Value = 150 -- Initial Value
	
	while true do -- Always do
		game.StarterGui.BasicGUI.Money.Text = Cash.Value .. "$"
	end

end)
5 Likes

Why does this thesis use StarterGui?

while task.wait() do -- Always do
                  wait(1)
		plr.PlayerGui.BasicGUI.Money.Text = Cash.Value .. "$"
	end
2 Likes

is it for NPC or Player?
I don’t know what you mean. get money after killing Player or Npc?

1 Like

in the script you use to do damage to npcs, put an if statement to check if the npc’s life is equal or lower than 0. if so, give a point to your leaderstats

small example

Humanoid:TakeDamage(50)

if Humanoid.Health <= 0 then --or basically Humanoid.Health == 0
player.Leaderstats.Score.Value += 1
end
3 Likes

not player NPC. i use starterGUI as i have a money display system

1 Like

But which player to give? if i add this script it will give cash to every player in game not the player i want to give

2 Likes

can you send the script you made?

1 Like

which script? the money giving one?

1 Like

yes

30char30char30char30char30char

1 Like

i dont know how to make it thats the thing.

1 Like

So when player1 kills player 2 then player 1 will get the money, is that what you mean?

1 Like

yes but not when a player kills another player. when player kills npc

1 Like
local _gNPC : Model = script.Parent

local _Humanoid : Humanoid = _gNPC.Humanoid

_Humanoid.Died:Once(function()
	local _agressorPlayer = _gNPC._Tagged.Value
	if not _agressorPlayer then
		return
	end
	
	_agressorPlayer.data.Cash.Value += 150
end)


local function _serverTag(_Aggresor : Player, _Victim : Model)
	if _Victim:FindFirstChild("_Tagged") then
		_Victim._Tagged.Value = _Aggresor
        return
	end
	
	local _newTag = Instance.new("ObjectValue")
	_newTag.Name = "_Tagged"
	_newTag.Value = _Aggresor
	_newTag.Parent = _Victim
	
	task.delay(30, function()
		_newTag:Destroy()
	end)
end

do
	--  a melee waepon somres osrt of
	VictimHumanoid:TakeDamage(25)
	_serverTag(_StarterAttacker, VictimHumanoid.Parent)
end

2 Likes

i will try using linked sword. 30char30char

1 Like

my linked sword works but how do i put the money giving script inside it??

1 Like

is the game 1 player or multi player?

1 Like

multiplayer 30char30char30char

1 Like

why does this work

	if humanoid.Health < 1 then
		print("enemy killed")
	end

but not this

	if humanoid.Health < 1 then
		game.Players.LocalPlayer.Data.Cash.Value += math.random(70,120)
	end
1 Like

its because you are adding the cash through a local script try sending a remote event to a server script to give the cash

1 Like

im not im using a script. i know it only works in local script but i dont know any other way. also how to fire a remove event???

1 Like