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

Why does my script give one player two weapons and the other none?

I'm back again, with the same script. This time however, I have a different problem.

For some reason, when both players spawn, it sometimes gives one player two weapons and the other none. What I am trying to do is give one player only one random weapon and the other player another random weapon from the Replicated Storage, not duplicates of one.

I also want to make sure the other player is given one weapon, and not left without one. For some reason though I can't get it working.

--Framework for Random Royale
--With help from scriptinghelpers.org

--//Variables
local rs = game:GetService("ReplicatedStorage")
local maps = rs:FindFirstChild("Maps")
local contestants = game.Players:GetPlayers()
local wep = rs.Weapons
local mapholder = workspace.MapHolder

--//Timing
local intermission = 15 --change to 30 after testing
local roundtime = 180 --maybe lengthen or shorten
local endtime = 10 --maybe 5 seconds









while true do
	mapholder:ClearAllChildren()
	wait(intermission)
	local contestants = game.Players:GetPlayers()
	local allmaps = maps:GetChildren()
	local newmap = allmaps[math.random(1,#allmaps)]
	local clonemap = newmap:Clone()
	clonemap.Parent = workspace.MapHolder
	print ("Map Loaded")
	wait(2)
	local spawns = clonemap:WaitForChild("Spawns")
	for _,player in pairs(game.Players:GetPlayers()) do
		if player and player.character then
			local spawnplace = spawns.Spawn
			local torso = player.Character:WaitForChild("Torso")
			if torso then
				torso.CFrame = CFrame.new(spawnplace.Position + Vector3.new(0,3,0))
				print ("Players Spawned")
				
				local backpack = player:FindFirstChild("Backpack")
				if backpack then
					local weps = wep:GetChildren()
					local randomwep = weps[math.random(1, #weps)]
					local clonewep = randomwep:Clone()
					local randomcontestant = contestants[math.random(1, #contestants)]
					clonewep.Parent = randomcontestant.Backpack
					print("Random Tool Given")
				end
			end
		end
	end
	wait(10)
	--give weapons
	--last man standing
	--restart	
end

The loop I am talking about starts on line 35, by the way.

1 answer

✓ Accepted answer

The loop on line 35 iterates through every player. Inside that loop, you're giving a random weapon to a random player. It seems like what you want to do is give the random weapon to the player in the loop and not a random player.

Remove line 48 and change the next line to clonewep.Parent = player.Backpack

1 comment

0

Why didn't I realize that? Thanks dude, got it working.

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