Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Im trying to make a area where you have a item and its giving me a error. Why?

Asked by 12 days ago
Edited 12 days ago

I am trying to make a area where it gives you the item when your in it and takes it when your not and it don't work pls help. Aso i am getting the error on the first code of " 17:55:17.115 - Workspace.Part.Script1:3: attempt to index nil with 'Backpack'"

while true do
    script.Parent.Touched:Connect(function()
        game.ServerStorage.BuildingHammer:MoveTo(Vector3.new(game.Players.LocalPlayer.Backpack))
    end)
    wait(1)
end

And

while true do
    script.Parent.TouchEnded:Connect(function()
        game.Players.LocalPlayer.Backpack.BuildingHammer:Moveto(game.ServerStorage)
    end)
    wait(1)
end

1 answer

Log in to vote
0
Answered by 12 days ago
Edited 12 days ago

I think you've got the whole concept of MoveTo wrong. First off you use MoveTo to move an object with a humanoid to a Vector3 position. For More Detail On 'Humanoid:MoveTo()' click this API link: Humanoid:MoveTo ROBLOX Developer You don't use it to move items into player's backpack, to move an object just parent it to a new place. E.g.

game.ServerStorage.BuildingHammer.Parent = --Player's Backpack

Also, i assume you are doing this in a server script. If this is true then local player won't work. Local Player only works for local scripts as local scripts are ran on the client, not the server so you use the playerAdded event like so:

game.Players.PlayerAdded:Connect(function(Player) --Save Player to a variable as first parameter

Also you don't have to put it in a while true do loop, there's just no point. You don't have to check for when the player touches the part constantly, the Touched event does that. Also using ''Region3'' would probably be recommended for this as you can detect whether a player is within a certain area. If you want to learn this you can watch the DevKing's Tutorial here: DevKing Advanced Series: Region3

Hope this helps!

~JeffTheEpicRobloxian

0
thank you so much cjkizzy286 24 — 12d
0
np JeffTheEpicRobloxian 216 — 12d
Ad

Answer this question