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

How to make in that script random item from lighting?

Hello everyone, Probably it's request, but i don't know how to make something like random weapon from lighing.

Here is script:

local debounce = false

function getPlayer (humanoid)
	local players = game.Players:children()
	for i = 1, #players do
		if players[i].Character.Humanoid == humanoid then return players[i] end
	end
	return nil
end


function onTouch(part)
	local human = part.Parent:findFirstChild("Humanoid")
	if (human ~= nil) and debounce == false then
		debounce = true
		local player = getPlayer(human)
		if (player == nil) then return end
		game.Lighting.SKP:clone().Parent = player.Backpack
		wait(5)
		debounce = false
	end
end

1 answer

✓ Accepted answer

You could do it using math.random(), like so:

local debounce = false

local function getPlayer (humanoid) -- You can (and should) localise functions, too.
    local players = game.Players:children()
    for i = 1, #players do
        if players[i].Character.Humanoid == humanoid then return players[i] end
    end
    return nil
end


local function onTouch(part)
    local human = part.Parent:FindFirstChild("Humanoid") -- findFirstChild is deprecated; use FindFirstChild instead.
    if (human ~= nil) and debounce == false then
        debounce = true
        local player = getPlayer(human)
        if (player == nil) then return end
	local weapons = game.Lighting:GetChildren()
	-- Note the changes here.	
	weapons[math.random(1, #weapons)]:Clone().Parent = player.Backpack
        wait(5)
        debounce = false
    end
end

Also, you might want to put the weapons inside a Folder inside ReplicatedStorage or ServerStorage instead of putting weapons in the Lighting.

1 comment

0

Thanks for help, works perfectly ;)

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