Error in giving gears

Hola, in the following code I intend it to be giving a gear:

local SpeedGearId = 99119158

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()

	local backpack
	while not backpack do
		backpack = character:FindFirstChild("Backpack")
		task.wait()
	end

	local speedGear = Instance.new("Tool")
	speedGear.Parent = backpack
	speedGear:Clone(workspace:WaitForChild("SpeedGearTemplate")):Parent(speedGear)
end)

But no error shows up, yet I have no gear. Help?

There’s a couple of things that’s preventing it from working

  • It never gets past the while not backpack do loop because it’s looking for the Backpack inside of the Character model, which doesn’t exist. As a result, the backpack variable remains nil, which causes it to continue running in a loop forever. The Backpack can be found when referencing that folder within the Player object, through Player.Backpack.

  • However, even after that is corrected, SpeedGearId is never referenced during the code, which means that when you create a clone of a newly created tool, nothing exists within that tool. Unless of course the SpeedGearTemplate that is referenced is the Handle of the Speed Coil gear, in which case it would make more sense to place that directly into a Tool while editing the game, and then create a clone of that (which is part of the example solution that I’ll explain below).

    If you’re referring to the Speed Coil item uploaded by Roblox, you would need to insert it into the game via InsertService:LoadAsset(). Although I’d recommend inserting it via the command bar while editing the game so it doesn’t have to repeatedly do this during runtime. Then, you could just create a clone of that item, similar to what the code is already doing, and then place it into the player’s Backpack.


Steps to fix it

First, you’d run the following in Roblox Studio’s Command Bar to insert the gear into the ReplicatedStorage

(Note: I am pretty sure that anything that is added into the game via InsertService:LoadAsset() is placed into a Model, so if you can’t find it, open up any newly created Models that were placed there).

-- Code to be run in the Roblox Studio Command Bar
local InsertService = game:GetService("InsertService")
local SpeedGearId = 99119158

local newlyCreatedGear = InsertService:LoadAsset(SpeedGearId)
newlyCreatedGear.Parent = game:GetService("ReplicatedStorage")
-- This adds the item into the game, placing it into the ReplicatedStorage
-- Make sure to take it out of the Model afterwards so it's just the Tool itself

After that, you can do something pretty similar to what was in the original codeblock, except you’d create a clone of the Speed Coil within the ReplicatedStorage, reference Player.Backpack, and then place that item there.

Alternatively, if you want this to be given to every player, every single time their character respawns, you can place it in their StarterGear folder. But if that’s the case, you might as well place it directly into the StarterPack, which would accomplish the same thing without any code. However, if you still want to accomplish this through a Script, the following code would be placed into a Server Script that preferably goes somewhere such as the ServerScriptService:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SpeedCoil = ReplicatedStorage:FindFirstChild("SpeedCoil") -- Adjust this to the actual name of the gear as it appears in the Explorer

local createNewSpeedCoilOnRespawn = false
-- Set this to true if you want it to give a new Speed Coil every respawn
-- This'll basically just determine if it goes in the Backpack or StarterGear

Players.PlayerAdded:Connect(function(player)
    local Backpack = player:WaitForChild("Backpack")

    local clonedSpeedCoil = SpeedCoil:Clone()
    clonedSpeedCoil.Parent = Backpack

    if createNewSpeedCoilOnRespawn == true then
        local StarterGear = player:WaitForChild("StarterGear")
        local newestClonedSpeedCoil = SpeedCoil:Clone()
        newestClonedSpeedCoil.Parent = StarterGear
    end
end)

If you have any questions about any of this, feel free to ask! :grin:

1 Like

I’m getting an error:

ServerScriptService.SpeedCoil:13: attempt to index nil with 'Clone'
in

local clonedSpeedCoil = SpeedCoil:Clone()

That error is indicating that what the SpeedCoil variable was referencing is nil (meaning it couldn’t find an object with that name), so once it tried to create a clone of it, it errored since it cannot clone something that doesn’t exist.

If the name of the object in the ReplicatedStorage is not exactly “SpeedCoil”, make sure you update how the SpeedCoil variable on line #4 of the Script looks for the object.


So if it was named “speedcoil”, it would become:

local SpeedCoil = ReplicatedStorage:FindFirstChild("speedcoil")

Or, if it had a space in its name, like “speed coil”, it would become:

local SpeedCoil = ReplicatedStorage:FindFirstChild("speed coil")

Same thing applies for the letters that are capitalized. So, if it’s in all caps, like “SPEED COIL”, it would become:

local SpeedCoil = ReplicatedStorage:FindFirstChild("SPEED COIL")



It is case-sensitive, which means that it must be spelled and capitalized in the exact same way as the name appears when viewing the object in the Explorer.

The gear ID is 99119158 and its called “Speed Coil” but im still getting that error. Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local SpeedCoil = ReplicatedStorage:FindFirstChild("Speed Coil") -- Adjust this to the actual name of the gear as it appears in the Explorer

local createNewSpeedCoilOnRespawn = true
-- Set this to true if you want it to give a new Speed Coil every respawn
-- This'll basically just determine if it goes in the Backpack or StarterGear

Players.PlayerAdded:Connect(function(player)
	local Backpack = player:WaitForChild("Backpack")

	local clonedSpeedCoil = SpeedCoil:Clone()
	clonedSpeedCoil.Parent = Backpack

	if createNewSpeedCoilOnRespawn == true then
		local StarterGear = player:WaitForChild("StarterGear")
		local newestClonedSpeedCoil = SpeedCoil:Clone()
		newestClonedSpeedCoil.Parent = StarterGear
	end
end)```

If possible, please share a screenshot of how it appears in the Explorer while editing the game. As long as it was placed in the correct spot and the spelling and capitalization matches up with the code in the Script, that error shouldn’t be happening, so there must be something else causing it to happen.

It isnt in explorer rather than replicated storage
image

What exactly are you trying to do here? :Clone() doesn’t pass any parameters

I saw it in another post where that was a solution and so i used it but I’m assuming it doesn’t work

Are you trying to clone the speedGear or the SpeedGearTemplate (and yes that doesnt work)

You need to take the Tool out of the Model and have it placed directly in the ReplicatedStorage.


But, since I saw that you updated the createNewSpeedCoilOnRespawn variable to true, you could have all of this handled automatically by placing it into the StarterPack, without needing to run any code from Scripts.

The gear, not the speed gear template

I’m losing track, therefore what? I’m sleep deprived so sorry

local SpeedGearId = 99119158

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()

	local backpack
	while not backpack do
		backpack = character:FindFirstChild("Backpack")
		task.wait()
	end

	local speedGear = Instance.new("Tool")
	speedGear.Parent = backpack
	local speedGearClone = speedGear:Clone()
	speedGearClone.Parent = workspace:WaitForChild("SpeedGearTemplate")
end)

In this code where should I put the coil

well im assuming off your old code that you want to parent the tool to the speedgeartemplate, i was just guessing soooo

so the tool should be its child? where should I put it?

If you still want to handle it through the Script, move the “Acceleration Coil” directly into the ReplicatedStorage by holding left click on it and moving it over the ReplicatedStorage. That way, it’s not placed into anything else; then when the Script tries to look for it, it’s in the exact spot it’s looking for. But if you do that, you’d need to rename it to “Speed Coil” or update the variable in the Script to reference “Acceleration Coil”.

But the other option is to just move the Acceleration Coil directly into the StarterPack Service, because any Tools placed there are automatically given to players every time they respawn, which does the same thing as the Script.

I put it in replicated storage updated the script for it to be called speed coil so nothing should be called Acceleration Coil but im not getting the coil, I also found an unkown global
image

Oh, I didn’t notice that the line of code for the Players Service was no longer included there when you last posted the codeblock.

Readd the following line of code to the very top of the Script so that it’ll know that “Players” is pointing to the Players Service:

local Players = game:GetService("Players")