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

How do i make it so that this drop system only gives the item to a specific player?

This is a server script and what i'm trying to accomplish is, once an npc has been killed it will drop a part. Once the part has been picked up it will give them an item, in this case a sword i'm not sure how to do it but this was my attempt. The sword just doesn't clone into the Backpack.

--//Services
local serverStorage = game:GetService('ServerStorage')

--//Variables
local weapons = serverStorage:WaitForChild('Weapons')
local myHum = script.Parent:WaitForChild('Humanoid')
local myHead = script.Parent:WaitForChild('Head')

--//Died
myHum.Died:Connect(function()
	local dropChance = math.random(0,3)
	local itemChance = math.random(0,5)
	
	if dropChance == 3 or 2 or 1 then
		local p = Instance.new('Part')
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(3,3,3)
		p.Position = myHead.Position
		p.Parent = game.Workspace
		
		p.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild('Humanoid') and hit.Parent.Name ~= 	script.Parent.Name then
				local clone = weapons:WaitForChild('Sword'):Clone()
				clone.Parent = hit.Parent:FindFirstChild('Backpack')
				p:Destroy()
			end
		end)
	end
end)

1 comment

0

serverStorage should be game.ServerStorage also shouldnt hit.Parent.Name and script.Parent.Name have something between them?

1 answer

✓ Accepted answer

Backpack is a child of the player object. You were trying to find it under the player's character. You can get the player using GetPlayerFromCharacter:

p.Touched:Connect(function(hit)
	local parentModel = hit.Parent
	if parentModel then 
		local player = game.Players:GetPlayerFromCharacter(parentModel)
		if player then 
			local weapon = weapons:WaitForChild("Sword"):Clone()
			weapon.Parent = player.Backpack
			p:Destroy()
		end
	end
end)

1 comment

0

Oh, thanks!

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