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

Have Kill For Money Script, But Gives To Wrong Player?

I really want a script so that if I was to kill you I would get money, here's the script I have so far, but the thing is in this script if I kill you, you get the money: game.Players.PlayerAdded:connect(function(player) local folder = Instance.new("Folder",player) folder.Name = "leaderstats" local currency1 = Instance.new("IntValue",folder) currency1.Name = "Cash" player.CharacterAdded:connect(function(character) character:WaitForChild("Humanoid").Died:connect(function() local tag = character.Humanoid:FindFirstChild("creator") if tag ~= nil then if tag.Value ~= nil then currency1.Value = currency1.Value + 10 --This is the reward after the player died. end end end) end) end) I've looked around and can't seem to find the answer. Can you please help me? Thank you for your time.

2 answers

✓ Accepted answer

Your code is almost right. Once you find the creator tag, it will hold the value of the person that killed us, so we need to find their leaderstats.Cash and add to it.

game.Players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats"
	local currency1 = Instance.new("IntValue",folder)
	currency1.Name = "Cash"
	
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			local tag = character.Humanoid:FindFirstChild("creator")
			if tag ~= nil then
				if tag.Value ~= nil then
					-- Here tag.Value is the player who killed us, so find their leaderstats
					local killersLeaderstats = tag.Value:FindFirstChild("leaderstats")
					if killersLeaderstats then
						-- Award the cash to the player that killed us
						killersLeaderstats.Cash.Value = killersLeaderstats.Cash.Value + 10
					end
				end
			end
		end)
	end)
end)

1 comment

0

thanks so much man, much appreciated!

Please use a code block instead of inline code.

As for the script, you are giving the player who died the coins instead of the player who killed. You should be getting the player who killed through the value of the creator IntValue inserted into the Humanoid:

game.Players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Model")
	stats.Name = "leaderstats"
	stats.Parent = plr
	local currency1 = Instance.new("IntValue")
	currency1.Name = "Cash"
	currency1.Parent = stats
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			local tag = char.Humanoid:FindFirstChild("creator")
			if tag and tag.Value and then
				plrWhoKilled = tag.Value
				if plrWhoKilled:FindFirstChild("leaderstats") and plrWhoKilled.leaderstats:FindFirstChild("Cash") then
					plrWhoKilled.leaderstats.Cash.Value = plrWhoKilled.leaderstats.Cash.Value + 10
				end
			end
		end)
	end)	
end)

Keep in mind this will only work with weapons that insert this creator value into the humanoid on kill.

3 comments

0

thanks for the tip on how to present my code as for the code itself, much appreciated!

0

if my Weapon doesn't have the "Humanoid" where should i put it in? i tried to put it in but the weapon killed it self :D

0

That script should be somewhere like ServerScriptService

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