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

How do I make this function take the arguments correctly?

Asked by 9 years ago

Okay, so here is my problem: there may be some dumb errors in it, but that's because I've been editing it so much to desperately fix it...

I'm making a game that relies on waves of enemies being spawned, I have this so far as a script:

Wave = 1

print("Wave Loaded")
function Create(item,amount,Xran1,Xran2,Zran1,Zran2,CPart)

for i = 1, amount do

            if item then
    local Creation = game.ServerStorage.Instances.item
    local Instance = Creation:Clone()               
    Instance.Parent = game.Workspace
    local xpos = math.random(Xran1,Xran2)           --To spawn grunts in an area
    local zpos = math.random(Zran1,Zran2)           
    Instance[CPart].CFrame = CFrame.new(xpos,50,zpos)
    wait(0.01)
            else
                print("Item is nil")                -- always prints this, never actually spawns grunt


end
end
end
wait(0.5)

print("Running function...")
Create(Grunt, 20, -78.44,-81.5, -27.71, -30.5, Torso) --Seems to not take grunt as an argument
print("Function ran")

whenever I run the create function, it seems to not take Grunt as an argument to spawn, I have no idea what to do and I've tried numerous things including have it do FindFirstChild instead, having it do waitForChild. An exception is replacing "Item" with "Grunt" in the actual function,which makes it work, so my guess is I'm doing something wrong with giving it the argument "Item", It does the same with the argument "CPart", where if I replace it with torso in the actual function, it works fine

If something like this was posted before, I have not been able to find it Any help would be greatly appreciated

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

Your problem is that when you write down game.ServerStorage.Instances.item it doesn't recognize item as a variable, but rather a decendant of game.ServerStorage.Instances. I suggest doing what you did with the CPart and changing that to game.ServerStorage.Instances[item], or you could use FindFirstChild. You will also have to change Grunt to "Grunt" and Torso to "Torso" so they are recognized as string values.

Another thing I found when testing it is that you put the Xran1/2 and Yran1/2 in the wrong spots. When using math.random, the smallest number must come first or else it won't function (-78.44 > -81.5 and -27.71 > -30.5).

Wave = 1

print("Wave Loaded")
function Create(item, amount, Xran1, Xran2, Zran1, Zran2, CPart)
    for i = 1, amount do
        if item then
            local Creation = game.ServerStorage.Instances[item]
            local Instance = Creation:Clone()               
            Instance.Parent = game.Workspace
            local xpos = math.random(Xran1, Xran2)           --To spawn grunts in an area
            local zpos = math.random(Zran1, Zran2)           
            Instance[CPart].CFrame = CFrame.new(xpos,4,zpos)
            wait(0.01)
        else
            print("Item is nil")                -- always prints this, never actually spawns grunt
        end
    end
end
wait(0.5)

print("Running function...")
Create("Grunt", 20, -81.5, -78.44, -30.5, -27.71, "Torso") --Seems to not take grunt as an argument
print("Function ran")
0
Thank you so much! It's strange that it works now, though, because I tried that, and tried switching the Xran and Zran values and it didn't work...either way, I'm happy it's finally working, thanks! LordTJcs 5 — 9y
0
No problem man! :) BlackJPI 2658 — 9y
Ad

Answer this question