Needing help with a portal script in a fighting game

Hello, I’m making a fighting game where you must enter a portal to enable “canFight”, and if canFight is off, you can’t punch other players. However, look at this.

the script

local portal = script.Parent
local plainsSpawnPoint = game.Workspace:WaitForChild("Islands"):WaitForChild("PlainsIsland"):WaitForChild("plainsSpawnPoint")	
local snowySpawnPoint = game.Workspace:WaitForChild("Islands"):WaitForChild("SnowyIsland"):WaitForChild("snowySpawnPoint")		-- Sets the variable to the corresponding SpawnPoint
local desertSpawnPoint = game.Workspace:WaitForChild("Islands"):WaitForChild("DesertIsland"):WaitForChild("desertSpawnPoint")
local Players = game:GetService("Players")

portal.Touched:Connect(function(touchedPlayer)
	if touchedPlayer and touchedPlayer.Parent:WaitForChild("Humanoid") then -- Determines if the touched object is a player
		local player = Players:GetPlayerFromCharacter(touchedPlayer.Parent)
		print(player)
		local canFight = player:FindFirstChild("canFight")
		if not canFight then
			print("nil")
		end
		if canFight then
			canFight.Value = true
			local character = touchedPlayer.Parent	-- Makes your life easier

			-- Lines of code below set the SpawnPoints' positions to their CFrames
			local plainsSpawnPointPosition = CFrame.new(plainsSpawnPoint.CFrame.X, plainsSpawnPoint.CFrame.Y + 5, plainsSpawnPoint.CFrame.Z)
			local snowySpawnPointPosition = CFrame.new(snowySpawnPoint.CFrame.X, snowySpawnPoint.CFrame.Y + 5, snowySpawnPoint.CFrame.Z)
			local desertSpawnPointPosition = CFrame.new(desertSpawnPoint.CFrame.X, desertSpawnPoint.CFrame.Y + 5, desertSpawnPoint.CFrame.Z)

			local RNG = math.random(1,3)	-- Determines an RNG number with math.random
			if RNG == 1 then	-- Determines if RNG = 1
				character:SetPrimaryPartCFrame(plainsSpawnPointPosition)	-- Sets the player's position to the spawn pad
			elseif RNG == 2 then	-- If RNG wasn't 1, detects if RNG = 2
				character:SetPrimaryPartCFrame(snowySpawnPointPosition)		
			elseif RNG == 3 then	-- If RNG wasn't 2, detects if RNG = 3
				character:SetPrimaryPartCFrame(desertSpawnPointPosition)
			end
			task.wait(1)
		end
	end
end)		

The explorer when I hit play:
Screenshot 2024-07-03 at 1.34.31 PM

The output:

WHAT DID I DO WRONG??? PLZ TELL ME!!!

1 Like

What you are doing wrong is here. The “canFight” is a part of the player’s character, not the player. You don’t need to use Players:GetPlayerFromCharacter(touchedPlayer.Parent)

Fixed code -

	if touchedPlayer and touchedPlayer.Parent:WaitForChild("Humanoid") then -- Determines if the touched object is a player
		local player = touchedPlayer.Parent
		print(player)

edit - based on your screenshots, i could be incorrect about this. however, you could add canFight to the player’s character on the server-side, and that should solve the problem.

I assume this is a server script, where is it located?

1 Like

the script is located inside the portal object

1 Like