Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why won't this script Fire?

Hi, Here is the script that won't fire or work at all.

Map1of4 = game.ServerStorage.Map1
Map2of4 = game.ServerStorage.Map2
Map3of4 = game.ServerStorage.Map3
Map4of4 = game.ServerStorage.Map4
game.Players.PlayerAdded:connect(function(plr)
	math.random(1,4)
	if 1 then
		Map1of4:clone(game.Workspace)
	end
	if 2 then
		Map2of4:clone(game.Workspace)
	end
	if 3 then
		Map3of4:clone(game.Workspace)
	end
	if 4 then
		Map4of4:clone(game.Workspace)
	end
end)

I have been trying to figure out whats going on here because I'm not getting any errors. Please help!!! THANKS!!!!

2 answers

local maps = game.ServerStorage["Maps"]:GetChildren() -- put all your maps in a Model named "Maps" in ServerStorage

random table item http://wiki.roblox.com/index.phptitle=Function_dump/Mathematical_functions#math.random

Note to devs: Links that span two lines don't work.

game.Players.PlayerAdded:connect(function() 
	maps[math.random(1, #maps)]:Clone().Parent = Workspace
end)

Yours fixed;

Map1of4 = game.ServerStorage.Map1
Map2of4 = game.ServerStorage.Map2
Map3of4 = game.ServerStorage.Map3
Map4of4 = game.ServerStorage.Map4
game.Players.PlayerAdded:connect(function()
    local randy = math.random(1,4) -- You need a variable to use your random value. 
    if randy == 1 then --use that variable to check against a number.
        Map1of4:Clone().Parent = Workspace --You don't put the Parent inside of  the parenthesis.
	elseif randy == 2 then --If you have more than one check, use elesif's, not new if statements.
        Map2of4:Clone().Parent = Workspace
	elseif  randy == 3 then
        Map3of4:Clone().Parent = Workspace
	else 
        Map4of4:Clone().Parent = Workspace
    end
end)

1 comment

0

Please include explanations of what you do rather than just posting code.

My way :

game.Players.PlayerAdded:connect(function() -- fires on player entry
	maps = game.ServerStorage.Maps:GetChildren() --create a model named maps
	randmap = mat.random(1,#maps) -- put all maps in that model
	clonedmap = randmap:Clone()
	clonedmap.Parent = game.Workspace
end)

Your way fixed ENJOY :

Map1of4 = game.ServerStorage.Map1
Map2of4 = game.ServerStorage.Map2
Map3of4 = game.ServerStorage.Map3
Map4of4 = game.ServerStorage.Map4

math.randomseed(tick()) -- makes numbers even more random!!! 

game.Players.PlayerAdded:connect(function()
    local rand = math.random(1,4) -- picks random number and makes it a variable
    if rand == 1 then -- checks random number
        Map1of4:Clone().Parent =game.Workspace
    elseif rand == 2 then-- checks random number
        Map2of4:Clone().Parent = game.Workspace
    elseif  rand == 3 then-- checks random number
        Map3of4:Clone().Parent = game.Workspace
    elseif rand == 4 then-- checks random number
        Map4of4:Clone().Parent = game.Workspace
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.