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 spawn my tool when I touch a brick?

I want to make an item spawn into the player's backpack when I touch a specific block, but when I touch it, it doesn't give me the tool. What am I doing wrong?

local plr = game.Players.LocalPlayer
script.Parent.Touched:connect(function()
	local gravity = game.ReplicatedStorage:WaitForChild("GravityCoil")
	gravity:Clone().Parent = plr.Backpack
end)

2 comments

0

Are you sure there's an object in ReplicatedStorage named "GravityCoil"?

0

yes i put one in replicated storage

1 answer

✓ Accepted answer

First off, it's important to know that LocalPlayer only runs in a LocalScript, and LocalScript is limited in where it can be used, compared to a server Script:

For more info and reference: http://wiki.roblox.com/index.php?title=API:Class/LocalScript

Second, in order to clone a tool to a player's backpack, you need to access that specific player's Character:

script.Parent.Touched:Connect(function(hit)
	
	-- Detects if the part that 'hit' the brick contains a Humanoid
	if hit.Parent.Humanoid then
		-- Function GetPlayerFromCharacter returns the Character associated with the player (hit.Parent)
	plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		-- Clones tool into player's backpack
	local gravity = game.ReplicatedStorage.GravityCoil:Clone()
	gravity.Parent = plr.Backpack
	
	end
end)

Hopefully this helps.

5 comments

0

can a local script go into a part like a wedge or a regular part

0

i dont know why but it not working i put it in one of my parts as a child of that part

0

To my and the wiki's knowledge, you can't use a LocalScript as a child of a Part, unless that Part is a Tool. You need to use this code in a server Script.

0

it gives the part like 1000 times, i've tried adding wait statements but it doesnt work

0

Fix: local db = false - 1st line : wait(2) - last line of script (9) : db == false 10th line as the wait statement is line 9.

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