I have a script within my NPC that correctly gives cash to the player when the player kills the NPC. I had to insert a script within the NPC to call a remote event, but I'm not quite sure how to do that with an actual player. I need to give 10 cash when a player kills another player. What am I doing wrong here? (FYI this is inside my save data, hence the other stuff in there.)

Look at lines 25-32. That is where I am checking if the player died to give money to whoever killed them.

local function onPlayerAdded(player)
	local playerKey = "Player_" .. player.UserId
	wait()
	print("User ".. player.UserId.. " Joined")

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats

	local myCash
	local success, err = pcall(function()
		  myCash = CashStore:GetAsync(playerKey) or STARTING_CASH
	end)
	if success then
		Cash.Value = myCash
		print("loading "..myCash)
	else
		print("failed to retrieve data")
		warn(err)
	end
	
	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)
	
	game.ServerStorage.Give.Event:Connect(function(Amount)
		wait()
		Cash.Value = Cash.Value + Amount
	end)
	
end
game.Players.PlayerAdded:Connect(onPlayerAdded)

3 comments

0

couple questions. Can you kill other players in your game? Question number 2, what weapon are you using to kill?

0

Yes I can kill other players in my game. I am using a "gun" but its really just a hitscan type thing where the mouse is the "bullet". So it's the cursor that clicks a player and that player takes damage basically.

0

I have tested if the gun works and it does just fine, I'm just not sure how to tell the server "hey player 1 was killed by player 2, give player 2 some money"