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

Why is this script not handling both duelers?

I have this part in my game script where it's supposed to call he "HandleDuelers" function on both of the duelers, but it's only handling one. I'm not sure why this is because I have the perimeters matched up to my knowledge. If you know how to fix this, I would be very grateful.

local function HandleDuelers(Dueler)
	local Spawns = Map:FindFirstChild("Spawns"):GetChildren()
	local RandomSpawn = table.remove(Spawns, math.random(1, #Spawns))
	
	Dueler.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
	
	local Sword = ServerStorage:FindFirstChild("Sword"):Clone()
	Sword.Parent = Dueler.Backpack
end

local PossiblePlayers = Players:GetPlayers()
local Player1 = PossiblePlayers[math.random(1, #PossiblePlayers)]
repeat Player2 = PossiblePlayers[math.random(1, #PossiblePlayers)] wait(1) until Player2 ~= Player1

HandleDuelers(Player1, Player2)

2 comments

0

First of all, you could make Dueler as a parameter, next thing, you need to make Player1 and Player2 global because functions are code blocks, so you would not be able to use that same variable outside that function

0

Could you make an answer showing me what you mean by "First of all, you could make Dueler as a parameter"?

1 answer

✓ Accepted answer

This is because your function can only take in 1 parameter. For example, this would not print "Hello world!"

function something(info)
	print(info)
end

something('Hello', ' world!')

You would have to do

function something(info, more)
	print(info, more)
end

something('Hello', ' world!')

For your case, your script would be

local function HandleDuelers(Dueler, SecondDueler)
	for _, Dueler in pairs({Dueler, SecondDueler}) do
	    local Spawns = Map:FindFirstChild("Spawns"):GetChildren()
	    local RandomSpawn = table.remove(Spawns, math.random(1, #Spawns))
	
	    Dueler.Character:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0, 3, 0))
	
	    local Sword = ServerStorage:FindFirstChild("Sword"):Clone()
	    Sword.Parent = Dueler.Backpack
	end
end

local PossiblePlayers = Players:GetPlayers()
local Player1 = PossiblePlayers[math.random(1, #PossiblePlayers)]
repeat Player2 = PossiblePlayers[math.random(1, #PossiblePlayers)] wait(1) until Player2 ~= Player1

HandleDuelers(Player1, Player2)

Hope this helps!

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