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()
This code I want to give swords to players , but it doesn't work when I call the function. Am I missing something? There is no error in the output on both the server and client, so I don't know whats going wrong. Thanks
Well, let's start:
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) -- you requested a player if player then -- if the player exists then do stuff 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 -- else do nothing end wait(5) giveSword() -- you must to set who is the player, else, player will be set as nil, which means false on an if statement
So you need to set the player:
giveSword(game.Players:WaitForChild("Playername"))
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) -- request the player from the event 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") -- v is the current player 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
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) -- request the player from the event 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)
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.
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") -- v is the current player 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()
Locked by NinjoOnline, davness, XToonLinkX123, and BSIncorporated
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?