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

How can i Make an item drop out of a certain NPC?

i am having trouble in here.

I tried to make it drop out of a NPC, but it works with all players.

the NPC I want the item to fall from's name is 'King Cake'(location:game.workspace)

the item I want it to make it drop from KingCake on death name is 'Chest1'(location:game.ReplicatedStorage)

my script appears to work on all players(it works in ROBLOX Studio, but not in the real game. Why?)

script info:

LocalScript(name:LocalScriptKC)(location:StarterGui)

my script:

local part = game.ReplicatedStorage.Chest1 --name of my thing
local plr = game.Players.LocalPlayer
local character = plr.Character
local humanoid = character.Humanoid

humanoid.Changed:connect(function()
 if humanoid.Health <= 0 then
  local clone = part:Clone()
  clone.Parent = workspace
  clone.Body.Position = character["Torso"].Position + 
         Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
  clone.Body.Rotation = Vector3.new(math.random(10, 100), 0, math.random(10, 100))
 end
end)

please help me. thanks

(sorry for bad grammar)

4 comments

0

Is the game filtering enabled?

0

no

0

did you use game:GetService("ReplicatedStorage")

0

no, i used 'local part = game.ReplicatedStorage.Chest1 --this is the name of the item i want it to drop when kingcake npc dies.

1 answer

✓ Accepted answer

Hey,

Try using the Died Event like so

humanoid.Died:Connect(function()
	local clone = part:Clone()
	clone.Parent = workspace
	--and so on
end

EDIT********************

Here is how i would set this up.

Insert a script into the npc model.

script:

local humanoid = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso") -- this is assuming the npc model has a Torso

humanoid.Died:Connect(function()
	local clone = game.ReplicatedStorage.Chest1:Clone()
	clone.Parent = workspace
	clone.Body.Position = torso.Position + Vector3.new(math.random(-5,5), 0, math.random(-5,5))
	
end)

EDIT AGAIN ***************************

So if you want to move a model with multiple pieces what you need to do is:

click the model and set the Property called "PrimaryPart" as one of the parts of the model (Usually the biggest or most center part)

Then if you want to move the whole model you can do:

local model = game.ReplicatedStorage.Model -- random example
model:SetPrimaryPartCFrame(CFrame.new(0,0,0)) -- the 0's are numbers that you must choose.

36 comments

0

but i want the chest to drop when a certain npc dies. how?

0

you can do the same thing if the npc has a humanoid in it

0

i dont want it to drop when a player dies, i dont know where to put the name of my npc. please help.

0

try putting a script inside of the npc with this code in it.

0

with the variables of course linking to its own humanoid and torso

0

alright ill try it, thanks

0

let me know how it works!

0

wait where do i put the (local)script?

0

doesnt work

0

You wont need a local script with this one.

0

All you need to do is put a script inside of the npc and have a variable for the humanoid of that npc. When the humanoid of that npc dies, then the chest should spawn on the npc's torso. If you are getting errors pls tell me what they say.

0

can you give me the whole script?because i am having trouble here. also, should i create a localscript or a script?i put a script inside the humanoid.

0

yea ill write another script for u real quick gimme a sec

0

alright bro thanks

0

reply to clone.body question:according to the guide i read on my phone, it said 'clone' should clone the item.

0

well, we are cloning Chest1. Is Chest1 a model? or is it a single part.

0

is Body the name of a part inside Chest1?

0

the chest is just a model.

0

Okay, so it is a model with multiple parts to it correct? (I need to know because you have to move the position of a model a certain way)

0

the model is just a part with a mesh,

0

okay sweet and the part is called Body right?

0

okay i updated the script

0

whoops, forgot to clone it. Now it is updated lol

0

the model is a part with a mesh,2 scripts(the first one is on touch destroy, and the other one is a money giver)

0

the end is underlined, i think its invalid.

0

oh sorry, put a ) at the end

0

still doesnt work

0

are you getting an error?

0

Just tested it on my server and it worked fine

0

the chests dont spawn :/

0

What does the error say i mean

0

wait!now it works! thank you dude!

0

1 more question, how can i make it so several parts sound?

0

spawn**

0

I'll write another edit onto the main post and tell you how, one sec. (Also, if i helped would you mind accepting the answer. ty!)

0

thanks for answering!

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