Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.

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)
0
Is it a Local script or a Server Script? BestCreativeBoy 1395 — 4y
0
it is server script. I edit a little bit. please check it back LikuLiku75 -1 — 4y
0
Well, if you test your script, it will error, possibly saying, Attempt to index nil by CharacterAdded BestCreativeBoy 1395 — 4y
0
so how to fix this? LikuLiku75 -1 — 4y
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. JesseSong 3916 — 4y

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?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

0
lemme check LikuLiku75 -1 — 4y
Ad