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

Roblox pick random player, give them sword script now working. Whats the issue?

So, I wrote a script to randomly pick a player (This seems to work fine, and it even prints the players name and prints it correctly) The issue is when the player is supposed to receive the sword. When this comes around, the sword is never given and I don't receive any error either and its in a normal script.

if match == 1 then
		wait(5)
		local picker = math.random(3)
		event = events[picker]
		print(event)
		text.Text = event
			if picker == 1 then
				local players = game.Players:GetPlayers()
				local nplrs = #players
				local Randomplayer = nil
				if nplrs > 0 then
					Randomplayer = players[math.random(1, nplrs)]
				end
				print(Randomplayer)
				local Clonetool = game.ReplicatedStorage.ClassicSword:Clone().Parent
				Clonetool = Randomplayer.Backpack

1 comment

0

print(Randomplayer) should be print(Randomplayer.Name) it might error

1 answer

✓ Accepted answer
math.randomseed(tick())
if match == 1 then
        wait(5)
        local picker = math.random(3)
        event = events[picker]
        print(event)
        text.Text = event
            if picker == 1 then
                local players = game.Players:GetPlayers()
                local nplrs = #players
                local Randomplayer = nil
                if nplrs > 0 then
                    Randomplayer = players[math.random(1, nplrs)]
                end
                print(Randomplayer)
                local Clonetool = game.ReplicatedStorage.ClassicSword:Clone()
                Clonetool.Parent = Randomplayer.Backpack

You had a .Parent after Clone() which is the error for this. This makes it so that Clonetool is the parent of the cloned sword, which is kinda confusing, but that's what is going on. You need to set the parent of the tool to the Randomplayer's backpack.

Also, I added in math.randomseed(tick()) which makes your numbers more random by generating a new seed every time the game is started up. If you don't have this, the same number will generate every time.

Hope this helps!

2 comments

0

Thanks so much. your a life saver

0

No problem! Glad to help

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