I am trying to make a teleport to a tower so i want to remove the player (who steps on the pad)'s inventory. here is the code, I am using a regular script because when I use the same code in a local script, It doesnt work and I get no errors. The error i get here is
Workspace.Doors.Door4.Teleport.Clear Gear:2: attempt to index nil with 'Backpack'
This is my code.
script.Parent.Touched:connect(function() local p = game.Players.LocalPlayer.Backpack p:ClearAllChildren() end)
This means it's on a server script, you can't see the local player on the server.
Also connect
is deprecated, you should use Connect
.
We can use the Touched argument we name hit
which is the object that touched it. As an object can't be a Model
and a character is composed of a model with object parts, we can simply just check if the parent of the object is a player's character.
Solution script:
local players = game.Players script.Parent.Touched:Connect(function(hit) local parent = hit.Parent local playerGet = players:GetPlayerFromCharacter(parent) -- returns player or nil if playerGet then -- is actually a player playerGet.Backpack:ClearAllChildren() end end)
You cannot use LocalPlayer in a server script. Try this script instead:
script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then -- make sure its a player that touched it local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- get the player in the game player.Backpack:ClearAllChildren() -- clear the backpacks children end end)