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

Why can't I get my tools when I teleported into a map?

I can't get my tools when I teleported into a map, why?

Here's how the code works: When Player1 equips a tool named "Wooden Sword", then he gets Wooden Sword when he teleported into the map, same goes to if Player2 equips a tool named "Sword", then he gets Sword when he teleported into the map.

--teleport code
local torso = player.Character:WaitForChild("Torso")
		local allspawns = math.random(1, #spawns)
		local randomspawn = spawns[allspawns]
		if randomspawn and torso then
			table.remove(spawns, allspawns)
			torso.CFrame = CFrame.new(randomspawn.Position + Vector3.new(0, 2, 0))
			-- tool giving code
			local inventory = game.Players.LocalPlayer.Inventory:GetChildren()
			local chosenitem = inventory(1, #inventory)
			chosenitem:Clone().Parent = player.Backpack

The teleport code works, just the tool giving code has a problem to operate, can anyone point out the wrong thing on the tool giving code?

2 comments

0

Are you using a Local Script in the tool giving script? If so, where is the script located?

0

I used Script and I put it in ServerScriptService.

1 answer

Only Local Scripts can access Local Player.

This is an important thing to remember. It's also important to know that Local Scripts don't work in Workspace or ServerScriptService. We will have to get the player a different way. I would suggest doing this server sided. To do this, I suggest the PlayerAdded event.

The Player Added event is an event for the Players Service. It returns the player when calling a function.

Example,

game.Players.PlayerAdded:connect(function(plr)
	local inventory = plr.Inventory:GetChildren()
	local chosenitem = inventory(1, #inventory)
	chosenitem:Clone().Parent = plr.Backpack
end)

Also, on line 3, you should be using square brackets to get the value of the table with it's index.

game.Players.PlayerAdded:connect(function(plr)
	local inventory = plr.Inventory:GetChildren()
	local chosenitem = inventory[1, #inventory]-- Square Brackets
	chosenitem:Clone().Parent = plr.Backpack
end)

If you want this to happen whenever the player respawns, use the CharacterAdded event.

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAdded:connect(function(char)
		local inventory = plr.Inventory:GetChildren()
		local chosenitem = inventory[1, #inventory]
		chosenitem:Clone().Parent = plr.Backpack
	end)
end)

Good Luck!

If I helped, please don't forget to accept my answer.

1 comment

0

Gg, it didn't worked.

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