How can i fix my script?

So i was making a script to give certain players a gear/tool and it works when i try it in play mode but when it’s in the actual game it doesn’t work. what am i doing wrong?

image

1 Like

Where does the script fail?
Are you running this in a LocalScript or Script?
does the print output “this works” without issue?

1 Like

Yeah it prints in output just fine

I believe the script fails here at this error: “Something unexpectedly tried to set the parent of to NULL while trying to set the parent of . Current parent is Backpack.”

It’s a regular script not local script. It’s supposed to pull gears from ServerStorage but it wont give the gear to the players in game but it does in roblox studio in test/play mode

You mispelled your name in the player table.

“Caliies” instead of “Calilies”

1 Like

Yes i’m aware of that i took this screenshot about 10 minutes before i fixed that. it still doesn’t gear the player in game but it does in studio. But thank you for letting me know! I appreciate it!

1 Like

I have just put a quick script together that works, its only edited for one name though however I am sure you can adjust it.

local Name = "Alexander_Ashford" (whatever name)
local tool = game.ServerStorage.X26 (locate the tool)


function onPlayerSpawned(player)
  if player.Name == Name then 
    tool:Clone().Parent = player.Backpack
  end
end


game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    onPlayerSpawned(player)
  end)
end)

Try it out if you like and tell me if it works (I have myself but you should of course check.)

1 Like

Hm. If I had to point out anything, it would be this:

gear:Clone().Parent = plr:WaitForChild("Backpack");

Try, when the player gets added, making a variable that directs to the backpack (backpack location never changes). Then, when the player respawns, copy it in with:

gear:Clone().Parent = theBackPack;

Then again, maybe the

gear:Clone().Parent

doesn’t sit well. You have to test.

(And it got solved. RIP.)

1 Like

local Players = {"xWil_l"} --Add your names here
local gear = game.ServerStorage["Ameature Level"]

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Car)
	for i = 1, #Players do
		if Players[i] == Player.Name then
			gear:Clone().Parent = Player.Backpack
		end
	end
	end)
end)

See if this works for multiple names, worked for me in studio and ingame

1 Like

This will work for specific players and can be placed in a module incase you want to disable a player’s permission in game.

local Players = {
    ["Calilies"] = false
}

game.Players.PlayerAdded:Connect(function(Client)
    if Players[Client.Name] then
        --give gear
    end
end)
2 Likes

A few of these replies are worrying me. Do remember that when you’re working with permission-based systems, you should avoid using a Player’s name; use their UserId instead. If you’re feeling lazy and don’t want to fetch UserIds, you can go ahead and use GetUserIdFromNameAsync which will return a UserId associated with a username, if the name has been used before.

Generally using a dictionary is better for your use case because it’s much easier to work with and it can adequately cover your use case as desired. See the code that @wevetments posted, though convert the key parts (the quotes between the square brackets) to UserIds without the quotes.

Also, for good practice and revisions, there’s a few things to point out. cc @xWil_l because your code shares the same issues.

  • GetService is a canonical way of fetching services. Don’t use dot syntax. Not only will you be unable to retrieve hidden services or ones named “Instance”, but service names can change as well. This will also lead to variable inconsistency when you start needing to use more services.
  • There’s no need to use a numeric for loop here. You can change this to a for pairs loop for far easier management, or use a dictionary. There’s not much difference in the operation, but there is in practice. You also don’t need index. If you use a dictionary without numerical indices, this will also fail to function properly. [1]

[1]:

local Table = {"Yes", "Hi", "No"}

-- No
for i = 1, #Table do
    local Member = Table[i]
    if Member__Something then
        whatever()
    end
end

-- Yes
for _, Member in pairs(Table) do
    if Member__Something then
        whatever()
    end
end

With this mind, that should give you a little perspective on the matter. Remember; it’s one thing to spoon feed code, but it’s another to actually understand what you’re being given or what’s on the inside. Having this knowledge is beneficial for your future endeavours in scripting.

I can provide you some code regarding it, reluctantly, since providing entire code is against my principles when it comes to replying to threads in Scripting Support. This one will keep your style of using usernames, but convert them into UserIds (so that if a name changes, you don’t have to really update your script):

-- Get your services so you can work with your assets
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

 -- Make your whitelist of users who should get your item and get the tool
local GearToGive = ServerStorage:WaitForChild("Ameature Level")
local GearWhitelist = {"Calilies", "SomeoneElse"}

-- Check for a player that joins, then operate on their Player
Players.PlayerAdded:Connect(function (Player)
    -- Connect a function to be ran each time a player spawns in with a character
    Player.CharacterAdded:Connect(function (Character)
        -- Search the whitelist and for each entry, get the UserId and check it
        for _, whitelistedUser in pairs(GearWhitelist) do
            local UserId = Players:GetUserIdFromNameAsync(whitelistedUser)
            if UserId == Player.UserId and Player:FindFirstChild("Backpack") then
                -- Give the gear! (if they have a backpack...)
                GearToGive:Clone().Parent = Player.Backpack
            end
        end
    end
end

On the other hand, given the above code, if you used a dictionary (which I absolutely adore for permission-based systems), you can completely get rid of that whole loop up there and instead just check if the name exists in the table directly. You’ll have to manually fill this with UserIds though. Here’s a repost of the above code but instead with a dictionary.

-- Get your services so you can work with your assets
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

 -- Make your whitelist of users who should get your item and get the tool
local GearToGive = ServerStorage:WaitForChild("Ameature Level")
local GearWhitelist = {
    ["Calilies"] = true,
    ["SomeoneElse"] = true,
}

-- Check for a player that joins, then operate on their Player
Players.PlayerAdded:Connect(function (Player)
    -- Connect a function to be ran each time a player spawns in with a character
    Player.CharacterAdded:Connect(function (Character)
        -- Check if the user's name is in our whitelist
        if GearWhitelist[Player.Name] then
            -- Give the gear! (if they have a backpack...)
            GearToGive:Clone().Parent = Player.Backpack
        end
    end
end

Please use UserIds though. Usernames can change, UserIds don’t.

2 Likes