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

How to make the server pick a random object to spawn?

Asked by 4 years ago
Edited 4 years ago

I'm trying to make it so that the server chooses a random number between 1 and 10 and using that number, spawn the correlating object. However, when the server tries to use the random number to spawn the object, I get this error:

22:44:39.473 - ServerScriptService.CoinSpawn:9: attempt to compare userdata with number

How can I avoid this? Here's the code:

local c1 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin1")
local c5 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin5")
local c10 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin10")

while wait(2) do
    local cInStor = workspace:WaitForChild("CoinStorage"):GetChildren();
    if (#cInStor <= 100) then
        num = Random.new(1,10)
    if num <= 5 then
        local Clone = c1:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    else if num >= 6 and num <= 9 then
        local Clone = c5:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    else if num == 10 then
        local Clone = c10:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    end
end
end
    else
        return
    end
end

EDIT: title spelling lol

1 answer

Log in to vote
2
Answered by
Leamir 3138 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Hello, ejones808!

If you want to get a random number, you use math.random(min,max) instead of Random.new

Please try identting your code

local c1 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin1")
local c5 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin5")
local c10 = game:GetService("ServerStorage"):WaitForChild("Coins"):WaitForChild("Coin10")

while wait(2) do
    local cInStor = workspace:WaitForChild("CoinStorage"):GetChildren() -- don't use ";"
    if (#cInStor <= 100) then
        num = math.random(1,10)
    if num <= 5 then
        local Clone = c1:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    else if num >= 6 and num <= 9 then
        local Clone = c5:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    else if num == 10 then
        local Clone = c10:Clone()
        Clone.Parent = workspace:WaitForChild("CoinStorage")
        Clone.CFrame = CFrame.new(math.random(-95, 125), (Clone.Size.Y/2), math.random(-91, 11))
    end
end
end
    else
        return
    end
end
0
didnt think of math.random lol. thank you for the help ejones808 34 — 4y
Ad

Answer this question