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?
There's a way easier way. Type "sword" in tool box, click at it then put it to starter pack. DONE!
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!