Condition problem in script

My code work perfectly until this point :

			if get_Character.Team == teams.Blue and get_Character.Character:FindFirstChild("Key") ~= nil or get_Character.Backpack:FindFirstChild("Key") ~= nil then

so basicly my code is just giving the key to red team player but the blue player team can get it too why?

Full script

for i, v in pairs(collection_Services:GetTagged("RedKeys")) do
	local debounce = false
	v.Touched:Connect(function(hit)
		local get_Character = player:GetPlayerFromCharacter(hit.Parent)
		if get_Character and get_Character.Character.Humanoid:GetState() ~= 'Dead' then
			if get_Character.Team == teams.Blue and get_Character.Character:FindFirstChild("Key") ~= nil or get_Character.Backpack:FindFirstChild("Key") ~= nil then
				return
			else
				if debounce == false then
					debounce = true
					
					local keyClone_sound = key_Sound:Clone()
					local clone_Keys = Keys:Clone()
					clone_Keys:SetAttribute("Team", "Red")
					clone_Keys.Parent = get_Character.Character
					keyClone_sound.Parent = get_Character.Character
					keyClone_sound.Playing = true
					print("Successfully cloned the red keys to the player")
					v.Transparency = 0.8

					task.wait(30)
					v.Transparency = 0
					debounce = false
				end
			end
		end 
	end)
end
1 Like

Could you show the output? (if there are any errors) also to fix I would recommend doing print debugging if that doesn’t work let me know.

there is no error the, the script supposed to give the team red player a key but it also gives to the team blue which are not supposed to happen. (Only red can get the key)

Try only checking to see if the player is on the blue team maybe?

for i, v in pairs(collection_Services:GetTagged("RedKeys")) do
	local debounce = false
	v.Touched:Connect(function(hit)
		local get_Character = player:GetPlayerFromCharacter(hit.Parent)
		if get_Character and get_Character.Character.Humanoid:GetState() ~= 'Dead' then
			if get_Character.Team == teams.Blue then
				return
			elseif get_Character.Team == teams.Red and get_Character.Character:FindFirstChild("Key") ~= nil or get_Character.Backpack:FindFirstChild("Key") ~= nil then
				if debounce == false then
					debounce = true

					local keyClone_sound = key_Sound:Clone()
					local clone_Keys = Keys:Clone()
					clone_Keys:SetAttribute("Team", "Red")
					clone_Keys.Parent = get_Character.Character
					keyClone_sound.Parent = get_Character.Character
					keyClone_sound.Playing = true
					print("Successfully cloned the red keys to the player")
					v.Transparency = 0.8

					task.wait(30)
					v.Transparency = 0
					debounce = false
				end
			end
		end 
	end)
end

Else try this
for i, v in pairs(collection_Services:GetTagged("RedKeys")) do
	local debounce = false
	v.Touched:Connect(function(hit)
		local get_Character = player:GetPlayerFromCharacter(hit.Parent)
		if get_Character and get_Character.Character.Humanoid:GetState() ~= 'Dead' then
			if get_Character.Team == teams.Blue then
				return
			elseif get_Character.Team == teams.Red and not get_Character.Character:FindFirstChild("Key") or not get_Character.Backpack:FindFirstChild("Key") then
				if debounce == false then
					debounce = true

					local keyClone_sound = key_Sound:Clone()
					local clone_Keys = Keys:Clone()
					clone_Keys:SetAttribute("Team", "Red")
					clone_Keys.Parent = get_Character.Character
					keyClone_sound.Parent = get_Character.Character
					keyClone_sound.Playing = true
					print("Successfully cloned the red keys to the player")
					v.Transparency = 0.8

					task.wait(30)
					v.Transparency = 0
					debounce = false
				end
			end
		end 
	end)
end

	

Try this.