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

Did this script going to work whenever players spawn or respawn?

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)

5 comments

0

Is it a Local script or a Server Script?

0

it is server script. I edit a little bit. please check it back

0

Well, if you test your script, it will error, possibly saying, Attempt to index nil by CharacterAdded

0

so how to fix this?

0

Please ensure you've given us sufficient information to answer your question. This will not be answered as the community does not know what you're are trying to achieve and will hence in a disparity. Always test and make sure you've inserted and implemented a script, with your code.

1 answer

✓ Accepted answer

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!

1 comment

0

lemme check

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