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

How can I make it so when a (any) player walks on top of a brick: their inventory is cleared?

I've tried altering a giver to instead ClearAllChildren from the player's backpack, and I've tried this recent code:

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

script.parent.Touched:connect(function(hit)--Touched event
    humanoid=hit.Parent:FindFirstChild("Humanoid")--Finding the Humanoid
    if humanoid ~=nil then--If the humanoid exists
		local player = getPlayer(humanoid)
		if (player == nil) then return end 
        player.Backpack:ClearAllChildren()--ClearsPlayerInventory
    end
end)

But even through many different kinds of articles and such about things similar to this, I can't figure it out. Any help? Thanks.

1 comment

0

You're over thinking this A LOT.

2 answers

✓ Accepted answer

I notice you're over complicating this, and i see two mistakes.

  1. Your whole GetPlayer function is not needed. Theres a thing called game.Players:GetPlayerFromCharacter(Character).
  2. you have script.parent instead of script.Parent (capital P).

How to do this the short way:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
    	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.Backpack:ClearAllChildren()
    end
end)

Any questions? Comment below ;D

Edit:

To clear the character from any tool we can do the following:

Loop through the character

Find all objects that have a className of "Tool"

Delete them

I'm adding this into the previous script.

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

	--clear backpack
    	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.Backpack:ClearAllChildren()

	--Remove any equiped tools.
		for _,v in pairs(hit.Parent:GetChildren()) do
			if v:IsA("Tool") then
				v:Destroy()
			end
		end
    end
end)

7 comments

0

For the most part, this does work. It takes away everything in the backpack UNLESS it's equipped, which I'll need as well. It may be better if I actually explain my situation, which I didn't do well before. I have two blocks ontop of eachother. One is a floor (bottom) one is a mat (top). I'm trying to make it so whenever you step on the floor it'll take all tools, and when you step on the mat it'l

0

Updated

0

That did fix the equipped tool issue, but do you know why it's still taking the tools when I'm no longer on the floor, and am on the mat? I still get tools on the mat, so I don't think it's the mat that's the issue, but they're near immediately taken away by the floor's script.

0

How big is your floor / mat?

0

I think the mat is 60x60, but it's thickness is .2 (so it's ontop of the floor, meaning the player will stand .2 above the floor when on the mat.

0

Make the mat ticker. with a height of 0.2 the legs will hit the floor through it. Make it at least 0.5 (And then put wedges at the side or something)

0

Thank you!

Here is a simple way to do it. You're overthinking this WAY too much, buddy. But it's okay, just keep watching tutorials and reading stuff on ROBLOX Lua until you feel comfortable and you feel good with it. But here is some help:

local clearinvenbrick = game.Workspace.ClearInvenBrick --[[ you can name the variable and brick anything you want, doesn't matter honestly.just rename and replace all the names and variables i'm putting if you want.--]]
clearinvenbrick.Touched:connect(function(hit)--function when touching brick
	local humanoidhealth = hit.Parent.Humanoid.Health--[[variable for finding humanoid's health--]]
	local humanoid = hit.Parent:FindFirstChild("Humanoid")--variable for finding humanoid
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) --[[variable for finding player--]]
	local backpack = player:FindFirstChild("Backpack") -- variable for finding backpack
	if humanoid and humanoidhealth > 0 then -- checking if it is a real player
	humanoid:UnequipTools() --unequips tools that are equipped
	backpack:ClearAllChildren()--takes tools away
end
end)

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