Cash for kill system

Trying to create a cash for kill system for my game

This is my datastores script

local DataStore = game:GetService("DataStoreService")

local moneydata = DataStore:GetDataStore("Coin")


game.Players.PlayerAdded:Connect(function(player)
	local playerKey = "Player_" .. player.UserId
	
	local data = Instance.new("Folder", player)
	data.Name = "Data"
	
	


	local money = Instance.new("IntValue")
	money.Parent = data
	money.Name = "Money"
	local myMoney
	
	local success, err = pcall(function()
		myMoney = moneydata:GetAsync(playerKey)
	end)
	
	if success then
		money.Value = myMoney
	else
		money.Value = 250
	end
	if money.Value < 0 then
		money.Value = 0
	end
	money.Value = moneydata:GetAsync(player.UserId) or 250
	moneydata:SetAsync(player.UserId, money.Value)
	money.Changed:connect(function()
		print("Saving data...")
		moneydata:SetAsync(player.UserId, money.Value)	
		print(player.UserId.."'s data has been saved!")

	end)
end)

What do I need to do to detect kills and give a player money per kill?

4 Likes

Hey, here is a tutorial that explains exactly how to do that. In the future, try doing a quick search to see if anything is similar to your topic, as there are many sources which can assist you.

4 Likes

Hey @crolaa! :wave:

You can easily just

image

  • Insert a script in ServerScriptService

  • Name it KillForCash

  • Now insert this script:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("NumberValue")
	cash.Value = 0
	cash.Parent = leaderstats
	
	
	player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value~= nil then
				leaderstats.Cash.Value = leaderstats.Cash.Value+10
			end
		end)
	end)
end)
3 Likes

Why if creator ~= nil? Its the same as if creator then

3 Likes

:man_shrugging: If the script doesn’t work you can do

if creator then

But, I’m sure it works…

4 Likes

Is there a way without it using the creator thingy