Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago

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

Log in to vote
0
Answered by
ryanzhzh 128
1 year ago
Edited 1 year ago

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!)

0
Doesn't help but thank you. DarkSide_091 0 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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
Log in to vote
0
Answered by
Puppynniko 1059 Moderation Voter
1 year ago

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

0
No it's not run each time. It always give me the same number DarkSide_091 0 — 1y
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 Puppynniko 1059 — 1y
Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
1 year ago
Edited 1 year ago

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)
Log in to vote
0
Answered by
enes223 327 Moderation Voter
1 year ago

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

Answer this question