In this script, it detects if an enemy has been touched, when that enemy is touched, it checks if it's touched by a player, and if that player is already in the battle. If the player is the first player to join, it creates a folder in ServerStorage and sets a value in the enemy and in the folder as the first player. When another person joins, it should check if that player has already been listed in the battle. However, when another person joins, it thinks the player has already joined, so it doesn't count it as another player. What am I doing wrong?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GuiEvent = ReplicatedStorage:WaitForChild("GuiEvent")


local battle = require(game.ServerScriptService.ModuleScripts.BattleScript)
local name = nil
script.Parent.Touched:Connect(function(part)
	print("enemy touched")
	if game.Players:GetPlayerFromCharacter(part.Parent) then
		if script.Parent.PlayersFighting.Value == 4 then
			print("the value is 4")
		elseif script.Parent.PlayersFighting.Value == 0 then
			print("setting this boy as player1")
			name = battle.GenerateName()
			script.Parent.PlayersFighting.Value = 1
			
			script.Parent.Player1.Value = part.Parent.Name
			battle.CreateFolder(part.Parent, script.Parent, name)
			part.Parent.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
			local player = game.Players:GetPlayerFromCharacter(part.Parent)
			player.PlayerStats.BattleFolder.Value = name
			print("hello")
			battle.BattleScript(player)

		else
			print("value is not 0 or 4")
			if part.Parent.Name == script.Parent.Player1.Value or script.Parent.Player2.Value or script.Parent.Player3.Value or script.Parent.Player4.Value then
				print("player name is equal to an existing player value")
				print(part.Parent.Name.. " is equal to player1 value or player2 value or player3 value or player4 value")
				print(script.Parent.Player1.Value)
				print(script.Parent.Player2.Value)
				print(script.Parent.Player3.Value)
				print(script.Parent.Player4.Value)
				--problem occurs here
			else
				print("player name isn't equal to an existing player value")
				print(part.Parent.Name)
				print("setting player up as a secondary player")
				part.Parent.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
				script.Parent.PlayersFighting.Value = script.Parent.PlayersFighting.Value + 1
			
				if script.Parent.Player2.Value == "" and script.Parent.Player1.Value ~= part.Parent.Name then
					
					script.Parent.Player2.Value = part.Parent.Name
					local player = game.Players:GetPlayerFromCharacter(part.Parent)
					player.PlayerStats.BattleFolder.Value = name
					
					battle.AddPlayer(part.Parent, name)
				elseif script.Parent.Player3.Value == "" and script.Parent.Player1.Value ~= part.Parent.Name and script.Parent.Player2.Value ~= part.Parent.Name then
					script.Parent.Player3.Value = part.Parent.Name
					local player = game.Players:GetPlayerFromCharacter(part.Parent)
					player.PlayerStats.BattleFolder.Value = name
					
					battle.AddPlayer(part.Parent, name)
				elseif script.Parent.Player4.Value == "" and script.Parent.Player1.Value ~= part.Parent.Name and script.Parent.Player2.Value ~= part.Parent.Name and script.Parent.Player3.Value ~= part.Parent.Name then
					script.Parent.Player4.Value = part.Parent.Name		
					local player = game.Players:GetPlayerFromCharacter(part.Parent)
					player.PlayerStats.BattleFolder.Value = name
					battle.BattleScript(player)
					battle.AddPlayer(part.Parent, name)
				end

			
			end
			

						
					
		end
	end
end)

Thanks!

1 comment

0

Line 09, trying saying "gane.Players:FindFirstChild(part.Parent.Name)" and othere lines too. Or it is the ModuleScript that is causing the problem