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

How do I give all players an item at a certain time?

Asked by 7 years ago
Edited by OldPalHappy 7 years ago

I have this script, but it only works when I test it, and not in the real game...

while true do
    wait(27)
    game.ServerStorage["LinkedSword"]:Clone().Parent = game.Players.LocalPlayer.Backpack
    wait(58)
end

Please help! I've looked everywhere but I get the same answer, and it never works!

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Please use code block in the future

Your problem is most likely that you are attempting to index the LocalPlayer from a server script.

Try parenting this code into StarterPack from a LocalScript and it should work.

But note - it will make duplicate swords so you should probably add a check. And localscripts can't access ServerStorage so would need to put the tool in ReplicatedStorage

local sword = game.ReplicatedStorage["Linked Sword"];
local plr = game.Players.LocalPlayer;

while true do 
    wait(27)
    if not plr.Backpack:FindFirstChild(sword.Name) then
        sword:Clone().Parent = plr.Backpack;
    end
    wait(58) 
end

Alternatively, if you want the whole server to recieve swords at the same time, you can use a generic for loop and iterate through the players from a script in Workspace.

local sword = game.ServerStorage["Linked Sword"];

while true do 
    wait(27)
    for _,v in next,game.Players:GetPlayers() do
        if not v.Backpack:FindFirstChild(sword.Name) then
            sword:Clone().Parent = v.Backpack;
        end
    end
    wait(58) 
end
Ad

Answer this question