Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this script not handling both duelers?

Asked by
R_alatch 394 Moderation Voter
6 years ago
Edited 6 years ago

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)
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 saSlol2436 716 — 6y
0
Could you make an answer showing me what you mean by "First of all, you could make Dueler as a parameter"? R_alatch 394 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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!

Ad

Answer this question