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 clone items to the local player through a server script?

What am I trying to do?: I am trying to make a script where when the Tomato Collides with the CuttingField and it gives the local player a cut tomato.

**What have I tried?:**I have tried many things. I have learned that using a local script doesn't work but I do not know how to get the local player without a local script. Every time I cloned it from a local script it broke the Item and it would not drop.

**More Info:**The cut tomato tool is in the ReplicatedStorage. The cutting field works and it triggers the function. I just need to know how to clone the item from the script and then transfer it to the local player.

Current Error Message: Players.TheHideousGloom.Backpack.Tomato.Script:7: attempt to index nil with 'Backpack'

Code:

local part = script.Parent.Handle
			local tool = game:GetService("ReplicatedStorage"):WaitForChild("Cut Tomato")
			local player = game.Players.LocalPlayer
			
		local function GiveTool()
			local toolCl = tool:Clone()
			toolCl.Parent = player.Backpack
		end
		
function OnTouch(otherpart)
	if otherpart.Name == "CuttingField" and script.Parent.Name == "Tomato" then
	GiveTool()
	end
end
part.Touched:Connect(OnTouch)

If you need anymore info I can give it to you. Thanks in advance.

1 answer

✓ Accepted answer

If the tomato is a tool in the players backpack then it is quite easy to get the player Ill show u the fixed script and point out the stuff

local part = script.Parent.Handle


        local function GiveTool()
local tool = game:GetService("ReplicatedStorage"):WaitForChild("Cut Tomato")
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent) -- this is the person
local toolCl = tool:Clone() -- U should put the local values always in the function as it is safer
toolCl.Parent = player:WaitForChild("Backpack") -- always use :WaitForChild()
-- dont forget to destroy the tomato tool afterwards
script.Parent:Destroy()
end

function OnTouch(otherpart)
    if otherpart.Name == "CuttingField" and script.Parent.Name == "Tomato" then
    GiveTool()
    end
end
part.Touched:Connect(OnTouch)

5 comments

0

Also that error means it doesnt know what is this backpack thing as it doesnt exist

0

Thanks for your reply, however, I got this error message: Players.TheHideousGloom.Backpack.Tomato.Script:6: attempt to index nil with 'WaitForChild'

0

oh heh i made a simple mistake lemme fix it real quick

0

Made a few adjustments and it worked. Thank you so much for your help!

0

it wasnt finding the player then i realised i was just giving it the tool for the :GetPlayerFromCharacter() so i made it script.Parent.Parent not script.Parent

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