Give object on killing

Hi, someone know a script for give a specific object if we kill a creature ?

Exemple: If i kill a zombie, he drop me a sword in my backpack

Thank you for your help :slight_smile:

1 Like

call the humanoid, at 0 health clone the tool to players backpack

You have a code ? i have try but it’s not working :confused:

Then you edit the OP to include the code, so we can help you with it

1 Like

The devforum is for help, not for people to create your projects for you.

2 Likes

I have this but it’s not work…

local Humanoid = script.Parent.Zombie

function PwntX_X()

local tag = Humanoid:findFirstChild("creator")

if tag ~= nil then

if tag.Value ~= nil then

local Leaderstats = tag.Value:FindFirstChild("status")

local gear = game.ReplicatedStorage.Items:WaitForChild('LaserGun')

if Leaderstats ~= nil then

Leaderstats.Money.Value = Leaderstats.Money.Value + 25 --Change Money to the stat that is increased.

gear:Clone().Parent = game.Players.LocalPlayer.Backpack  -- **I fixed this so it issues it to the player's backpack, not their name.**

end

end

wait(0.1)

script:Destroy()

end

end

i have been helped but nobody can script this function it’s so complicated

DevForum is not used for requesting a code from someone. Mainly DevForum exists to talk and help or explain a logic idea for programming something you can’t figure out how. Most of developers on this site are required to know basics of programming in LUA language, so you can actually understand how can you solve your problem.
I’ll give you an idea how to give any object to a player that defeated a mob (e.g. Zombie).

Make a folder of weapons in a Zombie creature or in a Replicated Storage. I would make a code that detects which player has defeated a Zombie. Doing so, you would clone the tool from the folder and place it in players backpack.

1 Like

Please use preformatted text for posting codes on DevForum.

Example:

print("Hello World")

i have edit my message thank you

1 Like

You’re defining a function called PwntX_X, but you’re not calling it in the script. It’s like building a car engine, but not turning it on.

New code:

local Humanoid = script.Parent.Zombie

function PwntX_X()
    local tag = Humanoid:findFirstChild("creator")

    if tag ~= nil then
        if tag.Value ~= nil then

            local Leaderstats = tag.Value:FindFirstChild("status")
            local gear = game.ReplicatedStorage.Items:WaitForChild('LaserGun')

            if Leaderstats ~= nil then
                Leaderstats.Money.Value = Leaderstats.Money.Value + 25 --Change Money to the stat that is increased.
                gear:Clone().Parent = game.Players.LocalPlayer.Backpack  -- **I fixed this so it issues it to the player's backpack, not their name.**
            end

        end

        wait(0.1)
        script:Destroy()
    end

end

PwntX_X() --Calls the function, so the code you've written actually runs

You have to get the player to give them the tool. LocalPlayer is something only accessible via LocalScripts, not Scripts. LocalScripts won’t run in the zombie that the script is in anyway. You just have to get the player that killed the zombie, and give them the tool.

Thanks for your help but it’s not work :confused: i have this error: attempt to index a nil value

It’s this lines:
local Leaderstats = game.Players:GetPlayerFromCharacter(tag.Value):FindFirstChild(“status”)
gear:Clone().Parent = game.Players:GetPlayerFromCharacter(tag.Value).Backpack

I have this now:

local Humanoid = script.Parent.Zombie

function PwntX_X()

local tag = Humanoid:findFirstChild("creator")

if tag ~= nil then

if tag.Value ~= nil then

local Leaderstats = tag.Value:findFirstChild("status")

local gear = game.ReplicatedStorage.Items:WaitForChild('LaserGun')

if Leaderstats ~= nil then

Leaderstats.Money.Value = Leaderstats.Money.Value + 25 --Change Money to the stat that is increased.

gear:Clone().Parent = game.Players:GetPlayerFromCharacter(tag.Value).Backpack

wait(0.1)

script:remove()

end

end

end

end

Humanoid.Died:connect(PwntX_X)

The money has given so it’s good, but the gear won’t go in backpack :confused:

Here, this code should work I didn’t realize the creator value could be used as the character instance. Your original code was almost correct.

local Humanoid = script.Parent.Zombie

function GiveReward()
	local tag = Humanoid:findFirstChild("creator")
	if tag ~= nil then
		if tag.Value ~= nil then
			--print(tag.Value)
			local Leaderstats = tag.Value:FindFirstChild("status")
			local gear = game.ReplicatedStorage.Items:WaitForChild('LaserGun')
			if Leaderstats ~= nil then	
				Leaderstats.Money.Value = Leaderstats.Money.Value + 25 
				gear:Clone().Parent = tag.Value.Backpack	
				wait(0.1)
				script:remove()
			end			
		end		
	end
end

Humanoid.Died:connect(GiveReward)
2 Likes

Thank you very much :slight_smile: