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

Remove tool from all players?

When the player is teleported back to the lobby in my round-based game, the sword in their inventory needs to be taken away.

How can I do that for all players?

I've tried looping through or removing it when they touch the lobby floor, and it DID remove it, but then they couldn't get it back because it was constantly removing it.

Code:

while true do
	wait(1)
	
	if gameMode == 1 then -- Intermission 
		-- set the intermissionTime to the intTimerlabel text ON BOTH LABELS
		interTimerLabel.Text = tostring(intermissionTime)
		interTimerLabel2.Text = tostring(intermissionTime)
		
		-- decrement intermissionTime
		intermissionTime = intermissionTime - 1
		
		-- if it hits 0, set it back to 30, switch gamemode, and teleport players
		if intermissionTime < 1 then
			-- change the current gameMode to round in progress (2)
			gameMode = 2
			
			intermissionTime = 31
			
			-- Give all the players in the game swords
			for _,player in ipairs(game.Players:getChildren()) do
				local tool = game.ReplicatedStorage.ClassicSword:Clone()
				tool.Parent = player.Backpack
			end
			
			-- teleport all the players to the arena
			target = CFrame.new(14, 1.715, 62.3) --could be near a brick or in a new area
			for i, player in ipairs(game.Players:GetChildren()) do
				--Make sure the character exists and its HumanoidRootPart exists
				if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
					--add an offset of 5 for each character
					player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
				end
			end
		end
		
	elseif gameMode == 2 then -- Match in progress
		-- set the arenaTimer text to matchTime
		arenaTimerLabel.Text = tostring(matchTime)
		
		-- decrement matchTime by 1
		matchTime = matchTime - 1
		
		-- if matchTime gets to 0, set it back to 180, switch gamemode, and teleport players
		if matchTime < 1 then
			-- change gamemode to intermission
			gameMode = 1
			
			intermissionTime = 31
			matchTime = 181
			
			-- sword is removed when the player touches the lobby floor
			
			-- teleport all the players to the LOBBY
			target = CFrame.new(24.5, 57.305, 50.4) --could be near a brick or in a new area
			for i, player in ipairs(game.Players:GetChildren()) do
				--Make sure the character exists and its HumanoidRootPart exists
				if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
					--add an offset of 5 for each character
					player.Character.HumanoidRootPart.CFrame = target + Vector3.new(0, i * 5, 0)
				end
			end
		end
	end
end

2 comments

0

Because it's a while loop, it constantly removes the tools. You should add a "break" when needed.

0

Where would I "break"?

1 answer

✓ Accepted answer

For line 51, you could make a function for the lobby floor when touched. For example:

local part = script.Parent

part.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChildWhichIsA("Humanoid") -- If finds a humanoid.
if human then
local player = game.Players:FindFirstChild(hit.Parent.Name) -- Checks if the humanoid is a player on game.Players
if player then
local gear = player.Backpack:FindFirstChild("SWORD NAME") -- Change "SWORD NAME" to the name of the sword
if gear then -- If the sword is on the player's backpack (unequiped).
gear:Destroy()
elseif not gear then -- If the sword is on the player's character (equiped)
player.Character:FindFirstChild("SWORD NAME"):Destroy() -- Again, change "SWORD NAME" to the name of the sword
end
end
end
end)

Place the script on the lobby floor and make it a script.

7 comments

0

server script

0

Thanks for your answer, it removes it when I'm not holding the sword, but if I'm holding it while getting teleported to the lobby, I still keep it. How could I remove it even if the player is holding it?

0

oh let me change that

0

how would i change the script to make sure it removes it if im holding it?

0

well if the player is holding the sword and they are teleported to the lobby, they should still lose the sword, since the script removes the sword if it's not on the player's backpack, so the script goes to the player's character and find the sword to remove

0

another solution is when the player touches the lobby floor their character gets Loaded (respawned) so they lose all their tools

0

nvm, i see u changed it, tysm this works!! :D

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