Well, let's start:
##1. No parameter, end!
The first issue you got was on calling function. You didn't set an parameter for giveSword function:
local replicatedstorage = game:GetService("ReplicatedStorage")
local weapons = replicatedstorage:WaitForChild("Weapons")
local woodensword = weapons:WaitForChild("WoodenSword")
local knightssword = weapons:WaitForChild("KnightsSword")
function giveSword(player)
if player then
local backpack = player:WaitForChild("Backpack")
if backpack then
local startergear = player:WaitForChild("StarterGear")
if startergear then
if player.Weapon.Value == "WoodenSword" then
woodensword:Clone().Parent = backpack
woodensword:Clone().Parent = startergear
elseif player.Weapon.Value == "KnightsSword" then
knightssword:Clone().Parent = backpack
knightssword:Clone().Parent = startergear
end
end
end
end
end
wait(5)
giveSword()
So you need to set the player:
giveSword(game.Players:WaitForChild("Playername"))
##2. Did you want to give sword for everyone, right?
To do that, you can use PlayerAdded event to apply that function to the new player:
local replicatedstorage = game:GetService("ReplicatedStorage")
local weapons = replicatedstorage:WaitForChild("Weapons")
local woodensword = weapons:WaitForChild("WoodenSword")
local knightssword = weapons:WaitForChild("KnightsSword")
game.Players.PlayerAdded:connect(function(player)
local backpack = player:WaitForChild("Backpack")
if backpack then
local startergear = player:WaitForChild("StarterGear")
if startergear then
if player.Weapon.Value == "WoodenSword" then
woodensword:Clone().Parent = backpack
woodensword:Clone().Parent = startergear
elseif player.Weapon.Value == "KnightsSword" then
knightssword:Clone().Parent = backpack
knightssword:Clone().Parent = startergear
end
end
end
end)
But if you want to give sword to everyone at a certain time, we must to call a for loop:
local replicatedstorage = game:GetService("ReplicatedStorage")
local weapons = replicatedstorage:WaitForChild("Weapons")
local woodensword = weapons:WaitForChild("WoodenSword")
local knightssword = weapons:WaitForChild("KnightsSword")
function giveSword()
for i, v in pairs(game.Players:GetChildren()) do
local backpack = v:WaitForChild("Backpack")
if backpack then
local startergear = v:WaitForChild("StarterGear")
if startergear then
if v.Weapon.Value == "WoodenSword" then
woodensword:Clone().Parent = backpack
woodensword:Clone().Parent = startergear
elseif v.Weapon.Value == "KnightsSword" then
knightssword:Clone().Parent = backpack
knightssword:Clone().Parent = startergear
end
end
end
end
end
##3. If statement for WaitForChild() ?
Statements requesting for a child provided by :WaitForChild() function are useless.
local replicatedstorage = game:GetService("ReplicatedStorage")
local weapons = replicatedstorage:WaitForChild("Weapons")
local woodensword = weapons:WaitForChild("WoodenSword")
local knightssword = weapons:WaitForChild("KnightsSword")
game.Players.PlayerAdded:connect(function(player)
local backpack = player:WaitForChild("Backpack")
local startergear = player:WaitForChild("StarterGear")
local weaponval = player:WaitForChild("Weapon")
if weaponval.Value == "WoodenSword" then
woodensword:Clone().Parent = backpack
woodensword:Clone().Parent = startergear
end
if weaponval.Value == "KnightsSword" then
knightssword:Clone().Parent = backpack
knightssword:Clone().Parent = startergear
end
end)
##4. Server or Client?
A thing you must to understand is that ReplicatedStorage is useful for LocalScripts, while if you use a Server Script, you should use ServerStorage instead.
#Final Script
Once you want to give the swords a certain time, here it goes:
local weapons = game:GetService("ServerStorage"):WaitForChild("Weapons")
local woodensword = weapons:WaitForChild("WoodenSword")
local knightssword = weapons:WaitForChild("KnightsSword")
function giveSword()
for i, v in pairs(game.Players:GetChildren()) do
local backpack = v:WaitForChild("Backpack")
local startergear = v:WaitForChild("StarterGear")
if v.Weapon.Value == "WoodenSword" then
woodensword:Clone().Parent = backpack
woodensword:Clone().Parent = startergear
elseif v.Weapon.Value == "KnightsSword" then
knightssword:Clone().Parent = backpack
knightssword:Clone().Parent = startergear
end
end
end
end
end
wait(5)
giveSword()
3 comments
Well, I'm trying to give each player a sword, so some players might be given woodenswords if their weaponvalue is set to "WoodenSword" or they might get the knight sword, and I don't want a player event, I just need when I call the function to give the players the right swords
oh so you want to give the sword at a certain moment?
yeah, I got it working though, thanks for the detailed explanation :) +1 & answer accepted!