Sword only going to one player

Hi so I am making a sword giver/remover and it works fine for one player, but when one player has it in their inventory no one else can get it, anyone know how to fix this?

local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = false
local debounce = false

arenaRestrictions.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not debounce then
		debounce = true
		if not givenSword then
			givenSword = true
			local newSword = classicSword:Clone()
			newSword.Parent = hit.Parent
			hit.Parent.HumanoidRootPart.CFrame = tpPart2.CFrame
		else
			for _, sword in pairs(hit.Parent:GetChildren()) do
				if sword:IsA("Tool") then
					sword:Destroy()
					givenSword = false
					hit.Parent.HumanoidRootPart.CFrame = tpPart1.CFrame
				end
			end
		end
		wait(1)
		debounce = false
	end
end)

In this case I recommend you’d do a table debounce system instead of a single debounce, as givenSword will be true for everyone if one player hits it

local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = {}
local debounce = false

arenaRestrictions.Touched:Connect(function(hit)
	local char = hit.Parent
	if char:FindFirstChild("Humanoid") and not debounce then
		debounce = true
		if not givenSword[char.Name] then
			givenSword[char.Name] = true
			local newSword = classicSword:Clone()
			newSword.Parent = char
			char.HumanoidRootPart.CFrame = tpPart2.CFrame
		else
			for _, sword in pairs(hit.Parent:GetChildren()) do
				if sword:IsA("Tool") then
					sword:Destroy()
					givenSword[char.Name] = nil
					char.HumanoidRootPart.CFrame = tpPart1.CFrame
					break
				end
			end
		end
		wait(1)
		debounce = false
	end
end)

Also, I recommend breaking the loop once a sword has been found to prevent unneeded checks

1 Like

Seems to be working on a local server, I will try with an actual human now.

You want to create the givenSword variable inside the Touched event, or if one player touches it and doesn’t get the sword for whatever reason they will only change the variable that was created when the part was touched, or it will be false forever.

Ok it doesn’t work with an actual human player.

What do you mean by actual human player? If it worked in a local server, then did you forget to publish?

By a human player I mean like in the actual game with a real player.

It worked once, but then didn’t let any of us even get a sword anymore.

Would a Remote Event work maybe? Once touched it fires the client then parenting it to their backpack?

Try changing the givenSword variable inside the Touched event, as I said.

Edit: Create it inside the event.

I tried that and it just kept giving the player swords when they touched the part and not removing them.

Edit: Yeah I put it in the event.

Wait what type of script is this? Server script or local script? Try printing out what givenSword[char.Name] contains. Also, when you were testing, were any of oyu holding your sword or no? Cause this only checks if you are holding it, not if it’s in your backpack

  1. Server script
  2. Ok I will try printing
  3. Yes we were holding the sword

The output prints true, and I can’t leave the arena when I don’t have it equipped.

Would you mind telling me how to check for if its in the backpack?

Maybe t ry something like this to check both?

local arenaRestrictions = script.Parent
local tpPart1 = arenaRestrictions.TPPart1
local tpPart2 = arenaRestrictions.TPPart2
local replicatedStorage = game:GetService("ReplicatedStorage")
local classicSword = replicatedStorage:WaitForChild("Tools").ClassicSword
local givenSword = {}
local debounce = false

arenaRestrictions.Touched:Connect(function(hit)
	local char = hit.Parent
	local plr = game.Players:GetPlayerFromCharacter(char)
	if plr and not debounce then
		debounce = true
		if not givenSword[char.Name] then
			givenSword[char.Name] = true
			local newSword = classicSword:Clone()
			newSword.Parent = char
			char.HumanoidRootPart.CFrame = tpPart2.CFrame
		else
			local sword = char:FindFirstChildOfClass("Tool")
			if tool and tool.Name == classicSword.Name then
				tool:Destroy()
			else
				for _, sword in pairs(plr.Backpack:GetChildren()) do
					if sword.Name == classicSword.Name then
						sword:Destroy()
						char.HumanoidRootPart.CFrame = tpPart1.CFrame
						break
					end
				end
			end
			givenSword[char.Name] = nil
		end
		wait(1)
		debounce = false
	end
end)

Ok it is working so far, just waiting for my friend to test it.

If you go to test at the top, you can create a server. This allows you to test with multiple players by yourself.

2 Likes