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

How to clone tools into a player backpack?

Asked by 5 years ago

Hello Everyone

I've been trying to create code where after an intermission time, a sword will be given to the player.

Here is a regular script (Not LocalScript) of the game loop that I made

Main = require(game.Workspace.ModuleScripts.Main)
local status = game.ReplicatedStorage:WaitForChild("Status")
local intermissionTime = 60
local roundTime = 120
local players = game.Players:GetPlayers()
local playerService = game.Players
local Number = intermissionTime

local sword = game.ServerStorage.Swords.BasicSword 

while true do
    for i = 1, intermissionTime, 1 do -- Countdown for intermission time
        status.Value = "INTERMISSION (" .. Number .. ")"
        wait(1)
        Number = Number - 1
    end
    Number = roundTime
    Main.ToBattleMap()

    for i = 1, #players do -- Attempting to clone and parent the clone to the player's backpack
        local player = players[i]
        player.CharacterAdded:Connect(function(Player)
            local backpack = player:WaitForChild("Backpack")
            local swordClone = sword:Clone()
            swordClone.Parent = backpack
        end)
    end

    for i = 1, roundTime, 1 do -- Countdown for Round Time
        status.Value = "ROUND IN PROGRESS (" .. Number .. ")"
        wait(1)
        Number = Number - 1
    end
    Main.ToLobbyMap()
    Number = intermissionTime
end

When I run it, The tool won't show up after the intermission time is over, but no errors are shown in the output. Can someone please tell me what is happening and how to fix this so it will show up?

Thanks in advance :)

1
from my experience you cant get tools from server storage maybe thats the problem? if it is try putting it in something else 3wdo 198 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

I'm not sure if this will solve your problem, but try putting the sword in replicated storage, that is what I usually do. I also saw that in line 22 for the paramater of the function you put player with a capital P. In line 23 however, you put player with a lower case p.

0
I put the sword folder in replicated storage but that also didn't work. Thanks anyway :) JustAnotherGaming 48 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I already see the issue here. When the games starts, it clears something from the ServerStorage, and it doesn't detect the item being in the ServerStorage, to prevent that from happening, you'd declare the variable by placing a duplicate of the Sword in ReplicatedStorage, but, keep the normal Sword in ServerStorage, write this code:

local sword = game.ServerStorage.Swords.BasicSword or game.ReplicatedStorage.Swords.BasicSword

That way, if the sword doesn't happen to be in ServerStorage, it automatically searches in the ReplicatedStorage.

0
When I Tried it, it still did not work, thanks anyway :) JustAnotherGaming 48 — 5y
Log in to vote
0
Answered by 5 years ago

Here you go!

local time = 0  
    local plrs = game.Players:GetChildren()
            local plr = plrs math.random(1,#plrs)
            for p = 1,#plrs do
                time = 120
                game.ReplicatedStorage.Sword.Parent = plrs[p].Backpack
                repeat
                    time = time - 1
                    wait(1)
                    until time == 0
0
When I tried it, it still didn't work, thanks anyway :) JustAnotherGaming 48 — 5y

Answer this question