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

How do I give players each a sword?

Asked by 3 years ago
local function swordfight()
    for i, v in pairs (game.Players:GetPlayers()) do
        wait()
        local linked = game.ServerStorage.sword:Clone()
        linked.Parent = v.Backpack
        wait(30) 
        local sword = v.BackPack.sword or game.Workspace:FindFirstChild(v.Name).sword
        sword:Destroy()
    end
end

This here is my script for giving players a sword. It works, yeah, but when I tested it out with my alt account, only the alt got the sword, not me. Any way to fix?

2 answers

Log in to vote
0
Answered by 3 years ago

There's a way easier way. Type "sword" in tool box, click at it then put it to starter pack. DONE!

0
Problem is he want to give sword to everyone and then destroy sword from everyone after 30 seconds by looking at the code. Somone_exe 224 — 3y
0
oh nikitos54327 70 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You're using wait(30) which delays 30 seconds for EACH loop.

This is why only one person got the sword (You have to wait 30 seconds until one person's sword is destroyed and another person receives it). Everything else is correct and there's no problem. You have to put the wait() function outside the loop then you have to create another loop to find the player's sword and delete them AFTER the wait() function.

You probably want to fit 2 functions inside 1 function if you're going to activate 1st function and wait 30 seconds before you activate 2nd function. Fixed code (Solution) will activate the initial function swordfight() that activate givesword() function then wait for 30 seconds before activate destroysword() function.

== Here's the solution ==

-- givesword() Give sword to all players
local function givesword()
    for i, v in pairs (game.Players:GetPlayers()) do
        wait()
        local linked = game.ServerStorage.sword:Clone()
        linked.Parent = v.Backpack
    end
end

-- destroysword() Destroy sword of all players
local function destroysword()
    for i, v in pairs (game.Players:GetPlayers()) do
        wait()
        local sword = v.BackPack.sword or game.Workspace:FindFirstChild(v.Name).sword
        sword:Destroy()
    end
end

-- swordfight() activate 2 functions in different time
local function swordfight()
    givesword()
    wait(30)
    destroysword()
end

-- activate swordfight()
swordfight()

In the future, you probably want to check if there's a function that delays the loop. All code inside the for loop will execute itself several times depending on the loop. For example, you can execute the loop 12 times (12 players) and you have to wait like 900 seconds for the last person to receive the sword.

Happy Scripting!

0
If you found this answer helpful (either directly OR indirectly) then you can accept this answer and help others who may be looking for same answer! Somone_exe 224 — 3y

Answer this question