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

Help with a function not working? [closed]

Asked by 9 years ago
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

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?

1 answer

Log in to vote
3
Answered by
davness 376 Moderation Voter
9 years ago

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) -- 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"))

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) -- 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

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) -- 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)

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") -- 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()
0
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 NinjoOnline 1146 — 9y
0
oh so you want to give the sword at a certain moment? davness 376 — 9y
0
yeah, I got it working though, thanks for the detailed explanation :) +1 & answer accepted! NinjoOnline 1146 — 9y
Ad