I did not try this script yet Did this script going to work whenever players spawn or respawn?
local RS = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local Folder = rp:WaitForChild("Folder") local function onCharacterAdded(character) local Tools = Folder:GetChildren() local ToolsTable = Tools local chosenTools = {} repeat local selectedIndex = math.random(1, #ToolsTable) table.insert(chosenTools, ToolsTable[selectedIndex]) table.remove(ToolsTable, selectedIndex) until #chosenTools >= 3 -- Numbers of tools going to give local plr = Players:FindFirstChild(character.Name) for i = 0, 3, 1 do local ToolsCloned = Folder:FindFirstChild(chosenTools[i]):Clone() ToolsCloned.Parent = plr.Backpack end end Players.LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
Well, LocalPlayer
can't be accessed by Server
scripts.
What's the fix?
There's an event called PlayerAdded
, which triggers when a player joins and useful for getting the player
.
This is what you must do, in place of Line 24
:
local function onAdded(player) player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns end Players.PlayerAdded:Connect(onAdded)
Also, there's a better method to get the player from the character, here, you are assigning plr
variable to it.
local plr = Players:GetPlayerFromCharacter(character)
Lemme know if it helps!
Closed as Non-Descriptive by JesseSong
This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.
Why was this question closed?