Max
Max

Reputation: 1

Script doesn't seem to be working, and I cannot understand why

script dont work

I am incredibly new to scripting, and I was just fooling around in ROBLOX studio and I made a script that was INTENDED to put an item stored in ReplicatedStorage into a specific players StarterPack, but no matter what I change it does not want to work. Could anyone explain why?

local playerName = "plr name"

local toolName = "tool name"

game.Players.PlayerAdded:Connect(function(player)
if player.Name == playerName then
local tool = game.ReplicatedStorage:FindFirstChild(toolName)
player.StarterPack:AddItem(tool)
end
end)

Upvotes: -1

Views: 151

Answers (2)

kno
kno

Reputation: 9

You are using an :AddItem() function which doesn't work. You must first clone the tool and add it into the players inventory.

Check the documentation on StarterPack if you are having issues: https://create.roblox.com/docs/reference/engine/classes/StarterPack

Try this:

local playerName = "plr name"
local toolName = "tool name"
local tool = game.ReplicatedStorage:FindFirstChild(toolName)

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == playerName then
            local newTool = tool:Clone()
            newTool.Parent = player.Backpack
        end)
 end`

The StarterPack is used to determine a set of Tools that all players will spawn with. If a developer wants to ensure that certain Tools are available to specific players, then they will need to parent the Tools directly to the player's Backpack instead.

Upvotes: 1

Kylaaa
Kylaaa

Reputation: 7188

Your code is mostly fine, but you didn't close your PlayerAdded function, and the StarterPack doesn't have an AddItem function.

If you are having issues, you can always check the documentation for the StarterPack. One issue that I think you'll run into is that putting a tool into the StarterPack will give it to all players, not just the one who matches the name.

The StarterPack is used to determine a set of Tools that all players will spawn with. If a developer wants to ensure that certain Tools are available to specific players, then they will need to parent the Tools directly to the player's Backpack instead.

So instead let's clone the tool into the player's Backpack. But since the backpack gets cleared every time the player respawns, let's move this code into the CharacterAdded event, so it fires every time the character loads into the Workspace.

local playerName = "plr name"
local toolName = "tool name"
local tool = game.ReplicatedStorage:FindFirstChild(toolName)

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == playerName then
        player.CharacterAdded:Connect(function()
            local newTool = tool:Clone()
            newTool.Parent = player.Backpack
        end)
    end
end)

Upvotes: 0

Related Questions