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

how to fix and make the script give a random number each time it's run?

I am trying to make a randomizing teleporter but the problem is that when using math.random and when I take the teleporter, it always teleport me to the same position. I am looking how to randomize each time the script is run.

local debounce = true

local team = game:GetService("Teams")["O5 Council"]

local sup = math.random(1,4)
script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 1 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(207.36, 288.05, 483.315))
				wait(0.5)
				debounce = true
				return sup
			end
		end
	end
end)

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 2 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(908.906, 122.288, 1034.539))
				wait(0.5)
				debounce = true
				return sup
			end
		end
	end
end)

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 3 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(1062.403, 202.746, 650.385))
				wait(0.5)
				debounce = true
				return sup
			end
		end
	end
end)

script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 4 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(970.813, 199.222, 672.873))
				wait(0.5)
				debounce = true
				return sup
			end
		end
	end
end)

5 answers

elseif is a keyword. Forgot this also idk how you did that:

local debounce = true -- Should be: local debounce = false

Full script if you apply it:

local debounce = false
local team = game:GetService("Teams")["O5 Council"]

local sup = math.random(1,4)

script.Parent.Touched:Connect(function(hit)
      if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 1 then
        if hit.Parent:FindFirstChild("Humanoid") then
            if debounce then
                hit.Parent:MoveTo(Vector3.new(207.36, 288.05, 483.315))
                wait(0.5)
                debounce = true
                return sup
            end
        end
   elseif game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 2 then
        if hit.Parent:FindFirstChild("Humanoid") then
            if debounce then
                hit.Parent:MoveTo(Vector3.new(908.906, 122.288, 1034.539))
                wait(0.5)
                debounce = true
                return sup
            end
        end
    elseif game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 2 then
        if hit.Parent:FindFirstChild("Humanoid") then
            if debounce then
                hit.Parent:MoveTo(Vector3.new(908.906, 122.288, 1034.539))
                wait(0.5)
                debounce = true
                return sup
            end
        end
elseif game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 3 then
        if hit.Parent:FindFirstChild("Humanoid") then
            if debounce then
                hit.Parent:MoveTo(Vector3.new(1062.403, 202.746, 650.385))
                wait(0.5)
                debounce = true
                return sup
            end
        end
    end
else if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and sup == 4 then
        if hit.Parent:FindFirstChild("Humanoid") then
            if debounce then
                hit.Parent:MoveTo(Vector3.new(970.813, 199.222, 672.873))
                wait(0.5)
                debounce = true
                return sup
            end
        end
    end
end)

(Code's kinda long sorry for any syntax errors!)

1 comment

0

Doesn't help but thank you.

Fixed your fix.

local debounce = true
local team = game:GetService("Teams")["YourTeamName"]
local randomizer = math.random(1,4)
script.Parent.Touched:Connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent).Team == team and randomizer == 1 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(0,0,0))
				wait(0.5)
				debounce = true
			end
		end
	elseif randomizer == 2 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(0,0,0))
			end
		end
	elseif randomizer == 3 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(0,0,0))
			end
		end
	elseif randomizer == 4 then
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce then
				hit.Parent:MoveTo(Vector3.new(0,0,0))
			end
		end
	-- repeat the elseif from there
	end
end

math.random(1,4) should be ran every time you hit because once the script ran sup would be the same and not change

2 comments

0

No it's not run each time. It always give me the same number

0

the variable it set to a number and you cant change it unless you use Math.Random Again and if the script re runs

Hello, I've noticed your code is super unorganized and you're not checking if it's a humanoid that touched it cause anything else can touch it and run your code so when you use it, it won't work!

So here's a new code for you!

local PlayersService = game:GetService("Players")

local debounce = false
local team = game:GetService("Teams")["O5 Council"]

local sup = math.random(1,4)

local positions = {
	["1"] = Vector3.new(207.36, 288.05, 483.315);
	["2"] = Vector3.new(908.906, 122.288, 1034.539);
	["3"] = Vector3.new(1062.403, 202.746, 650.385);
	["4"] = Vector3.new(970.813, 199.222, 672.873);
}

function teleportPlr(pos, hit)
	
	hit.Parent:MoveTo(pos)
	wait(0.5)
	debounce = true
	
end

script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChild("Humanoid") and PlayersService:GetPlayerFromCharacter(hit.Parent) and debounce == false then
		
		local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
		
		if player.TeamColor == team then
			for index, vector in pairs(positions) do
				if sup == index then
					
					teleportPlr(vector, hit)
					
				end
			end
		end
	end
end)

hey you! have you ever heard of enes? if you are in trouble, better call enes!

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