Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can I spawn my tool when I touch a brick?

Asked by 7 years ago
Edited 7 years ago

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)
0
Are you sure there's an object in ReplicatedStorage named "GravityCoil"? antonio6643 426 — 7y
0
yes i put one in replicated storage supercoolboy8804 114 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

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.

0
can a local script go into a part like a wedge or a regular part supercoolboy8804 114 — 7y
0
i dont know why but it not working i put it in one of my parts as a child of that part supercoolboy8804 114 — 7y
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. block01352 57 — 7y
0
it gives the part like 1000 times, i've tried adding wait statements but it doesnt work wolffdonnaven 12 — 4y
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. Noxcitrus 2 — 4y
Ad

Answer this question