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

Why does my function not get called in local script?

It's a local script. There's no errors in the output when running in Play mode. The Script is located under the ServerScriptService. This script's goal is to remove the player's tool and move it to somewhere else other than the starter pack(all of this happening while in play mode). But instead of this happening this bug lets the tool remain in the player's backpack(While in Play mode). I also tried debugging by adding a print function under the first line of Code(game.Players.PlayerAdded:Connect(function(), but the printed string never showed up meaning the function didn't get called.

game.Players.PlayerAdded:Connect(function()
	local Player = game:GetService("Players")
	local Backpack = game.Players.PlayerName.Backpack
	local InBackPack = Backpack:FindFirstChildWhichIsA("Tool")
	InBackPack:Destroy()
end)

1 answer

ok so uh i see a few problems.

  1. LocalScripts only work when you put in starterplayerscripts, startercharacterscripts, startergui and also replicatedfirst put anywhere and it will not work.
  2. in line 3, you didn't correctly reference the player that joined.
  3. You shouldn't be using a local script for server sided changes.
  4. Why did you use game.Players when you already referenced game.Players at line 2 :v

here is the script you should've done (it is a normal script)

game.Players.PlayerAdded:Connect(function(player) -- the player parameter will reference the player who joined
	local Backpack = player:WaitForChild("Backpack")
	for i,v in ipairs(Backpack:GetChildren()) do -- get all children of the backpack (i is iteration, v is value)
		if v:IsA("Tool") then
			v:Destroy()
		end
	end

2 comments

0

to reference the player (IN LOCAL SCRIPTS), you do game.Players.LocalPlayer

0

Oh I see, thanks for stopping by to answer my question!

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