#why wont the part spawn after clicking the button?

1 messages · Page 1 of 1 (latest)

knotty cloak
#
local replicatedStorage = game.ReplicatedStorage

local roundtime = 120
local button = game.ReplicatedStorage:FindFirstChild("button")
local function rng()
    local rngValue = math.random(17,17) --placeholder so the button spawns 100% of the time
    if rngValue == 17 then
        if button then
            local buttonClone = button:Clone()
            buttonClone.Parent = game.Workspace
            local randomposition = Vector3.new(math.random(-52,52), 3, math.random(-52,52))
            buttonClone:PivotTo(CFrame.new(randomposition))
            task.wait(0.1)
            local main = button.main
            local press = main.ClickDetector
            if press then
                press.MouseClick:Connect(function()
                    local part = Instance.new("part",workspace)
                    part.Position = Vector3.new(math.random(-52,52), 3, math.random(-52,52))
                    part.Anchored = false
                end)
            end
            
        end
    end
end

for i = 1, roundtime do 
    rng()
    task.wait(1)
end
flint larkBOT
#

studio** You are now Level 6! **studio

knotty cloak
#

the buttons are spawning in as i want, but when i click on them the part wont spawn in

solemn nimbus
#

Can collide is on?

knotty cloak
#

it is on

solemn nimbus
#

i think when u create a new part instance p has to be capitalized

#

that might be it @knotty cloak

mystic sun
#

yea

#

thats it

knotty cloak
#

no way its that simple

mystic sun
#

a small mistypo

knotty cloak
#

yeah still doesnt work

mystic sun
#

hm

solemn nimbus
#

i felt so smart for catching that

#

but it wouldve been a problem anyway

#

does the part appear in workspace in the explorer

mystic sun
#

local press

solemn nimbus
#

yes it will be a local part

#

which should still work

knotty cloak
#
if press then
                press.MouseClick:Connect(function()
                    local part = Instance.new("part",workspace)
                    part.Position = Vector3.new(math.random(-52,52), 3, math.random(-52,52))
                    part.Anchored = false```
cobalt harness
#

shouldnt it be .mousebutton1click

knotty cloak
#

this part doesnt work i think

solemn nimbus
#

its a clicketector

cobalt harness
#

i mightyoh be in my

#

Oh

solemn nimbus
#

clickdetectors use mouseclick

knotty cloak
cobalt harness
#

i thought he meant a gui button

solemn nimbus
#

i am very smart

knotty cloak
cobalt harness
mystic sun
#

try this

`local replicatedStorage = game.ReplicatedStorage

local roundTime = 120
local button = replicatedStorage:FindFirstChild("button")

local function rng()
local rngValue = math.random(17, 17) -- Always spawns for now
if rngValue == 17 and button then
local buttonClone = button:Clone()
buttonClone.Parent = workspace
local randomPosition = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
buttonClone:PivotTo(CFrame.new(randomPosition))

    task.wait(0.1)
    
    local main = buttonClone:FindFirstChild("main")
    if main then
        local press = main:FindFirstChild("ClickDetector")
        if press then
            press.MouseClick:Connect(function()
                local part = Instance.new("Part")
                part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                part.Anchored = false
                part.Parent = workspace
            end)
        end
    end
end

end

for i = 1, roundTime do
rng()
task.wait(1)
end`

solemn nimbus
#

no way that changes anything

knotty cloak
#

what did you change?

solemn nimbus
#

he added an if statement

knotty cloak
#

holy shit it works

mystic sun
#

local press =

#

local main

#

local main = button.main
this accessed the original button

#

not the cloned one

knotty cloak
#

oooh

dark granite
#

U used pivotto for a vector3

mystic sun
#

so main doesnt refer to the one in workspce

dark granite
#

Pivot is only for cframe

solemn nimbus
#

nuh uh

dark granite
#

Replace it with moveto()

knotty cloak
dark granite
knotty cloak
dark granite
#

Let me check

knotty cloak
#

i think moveto() doesnt work on models

solemn nimbus
#

you can pivot with both properties of cframes

dark granite
#

Send ss of output

solemn nimbus
#

position and orientation

#

which means u can use vector3s

#

no kizzy

dark granite
#

NUH UH

#

Cframe only!!!

mystic sun
#

why bother helping anymore

dark granite
flint larkBOT
#

studio** You are now Level 6! **studio

dark granite
#

Moveto is server sided dawg

knotty cloak
#

also while im still here

#

i need the part to have 50% of spawning

#

any ideas:>

solemn nimbus
#

math.random

#

if math.random(0,1) == 1 then

knotty cloak
#
local rng2 = math.random(1,2)
        if rng2 == 2 then
            local main = buttonClone:FindFirstChild("main")
            if main then
                local press = main:FindFirstChild("ClickDetector")
                if press then
                    press.MouseClick:Connect(function()
                        local part = Instance.new("Part")
                        part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                        part.Anchored = false
                        part.Parent = workspace
                    end)
                end
            end
        end```
#

this is what i tried to do

knotty cloak
solemn nimbus
#

yes yes feel free to tell all your friends how smart i am twin

knotty cloak
#

there isnt a 50%

#

or either im really fucking lucky

solemn nimbus
#

oh i actually dont think 0 is a valid input sorry

flint larkBOT
#

studio** You are now Level 9! **studio

solemn nimbus
#

do math.random(1,2) == 1 i think

knotty cloak
#

so uhhh

#

if i put "math.random(1,2) == 1" it wont spawn any parts

#

but if its "math.random(1,2) == 2" then it will always spawn

solemn nimbus
#

show me

#

make sure ur generating a new number every time

#

you might only be generating one number

#

@knotty cloak

knotty cloak
#

oh sorry

#

give me asec

#
if math.random(1, 2) == 2 then
            print("2")
            local main = buttonClone:FindFirstChild("main")
            if main then
                local press = main:FindFirstChild("ClickDetector")
                if press then
                    press.MouseClick:Connect(function()
                        local part = Instance.new("Part")
                        part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                        part.Anchored = false
                        part.Parent = workspace
                    end)
                end
            end
        elseif math.random == 1 then
            print("1")
        end```
#

i added print to see what it picks

#

it keeps picking 2

solemn nimbus
#

your indentation looks screwwy

#

is that how it is in studio?

knotty cloak
#

no

knotty cloak
solemn nimbus
#

no just make it else

#

instead of elseif

#

because rn its picking 2 different random numbers

#

every time you call math.random its a different random number

knotty cloak
#

even without the "else if" part it keeps picking 2

solemn nimbus
#

wdym

#

it prints 2 every time?

#

like how many times did you try it

knotty cloak
#

it hasnt chose 1 once

solemn nimbus
#

well thats a 3% chance

#

which is unlikely but not impossible if you think about it

knotty cloak
#

okay it works now with 1

#

but its still 100% no matter if its == 1 or ==2

solemn nimbus
#

js to make sure make it a variable in the line before

#
local random = math.random(1,2)
if random == 1 ...```
knotty cloak
#

okay so i noticed something

#

it picks 1 or 2 normally its 50% chance

#

but its 50% it will spawn blocks every time or 50% it wont even spawn a single one

mystic sun
#

change it to this
local randomChance = math.random(1, 2)
if randomChance == 2 then

solemn nimbus
#

bro you are not helping

#

that is the same

#

random is not Random

mystic sun
#

and then

#

and then

#

wait im not finished

solemn nimbus
#

💀

#

send me a screenshot of the whole script js to make sure theres not a mistake elsewhere

knotty cloak
#
local replicatedStorage = game.ReplicatedStorage

local roundTime = 120
local button = replicatedStorage:FindFirstChild("button")

local function rng()
    local rngValue = math.random(17, 17) -- Always spawns for now
    if rngValue == 17 and button then
        local buttonClone = button:Clone()
        buttonClone.Parent = workspace
        local randomPosition = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
        buttonClone:PivotTo(CFrame.new(randomPosition))

        task.wait(0.1)
        local random = math.random(1,2)
        if random == 2 then
            print("2")
            local main = buttonClone:FindFirstChild("main")
            if main then
                local press = main:FindFirstChild("ClickDetector")
                if press then
                    press.MouseClick:Connect(function()
                        local part = Instance.new("Part")
                        part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                        part.Anchored = false
                        part.Parent = workspace
                    end)
                
                end
            end
        end
    end        
end

for i = 1, roundTime do 
    rng()
    task.wait(1)
end```
solemn nimbus
#

and if you put else on the same like as the if random == 2 then it doesnt print anything?

knotty cloak
#

alr nvm i know why it wont work

#

it goes math.random once and then it decides for the rest of the script

#

i think i would have to use a loop for it

#

crashed my studio by accident 👍

mystic sun
#

okay im back

knotty cloak
#

bruh

#

cant send a video

#

WHY CANT I SEND IT IN VIDEO FORMAT

solemn nimbus
#

i downloaded it its fine

#

why are you a whale

knotty cloak
solemn nimbus
#

whats the problem

knotty cloak
#

like 50% chance it spawns in 1 part

#

not 100% of spawning hundredes of them 😭

mystic sun
#
local replicatedStorage = game.ReplicatedStorage

local roundTime = 120
local button = replicatedStorage:FindFirstChild("button")

local function rng()
    local rngValue = math.random(17, 17) 
    if rngValue == 17 and button then
        local buttonClone = button:Clone()
        buttonClone.Parent = workspace
        local randomPosition = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
        buttonClone:PivotTo(CFrame.new(randomPosition))

        task.wait(0.1)

        local randomChance = math.random(1, 2)
        if randomChance == 2 then
            print("2")
            local main = buttonClone:FindFirstChild("main")
            if main then
                local press = main:FindFirstChild("ClickDetector")
                if press then
                    press.MouseClick:Connect(function()
                        local part = Instance.new("Part")
                        part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                        part.Anchored = false
                        part.Parent = workspace
                    end)
                end
            end
        elseif randomChance == 1 then
            print("1")
        end
    end        
end

for i = 1, roundTime do 
    rng()
    task.wait(1)
end```
#

try this

knotty cloak
#

so it fixed the hundreds problem

#

but it didnt fix the 100% problem

#

lmao 3 devs vs 1 script

mystic sun
#

so u want it to be 50%

#

?

knotty cloak
#

yeah

#

exactly

#

50% of spawning a part

mystic sun
#

i see the math.random(17, 17

#

has only range from 17 to 17

#

instead of 1 to 17

knotty cloak
#

its a place holder

mystic sun
#

change it to 1, 17

#

try

#

if it works

knotty cloak
#

its gonna be 1, 20 but i dont want to wait for it to spawn\

mystic sun
#

or put it 8, 17

#

so its 50%

#

close to it

knotty cloak
#

the math.random(17, 17) is about spawning the button

mystic sun
#

oh

#

oh i see

knotty cloak
#

its gonna be 5% to spawn in the button and then 50% for the button to spawn a part

mystic sun
#

that clears alot

#

okay

#

u dont have random chance variabled

#

nvm

#

wait

#

im blind just

knotty cloak
#

yo we need more scripters on this 😭

flint larkBOT
#

studio** You are now Level 7! **studio

mystic sun
#

think i fixed it

#

imma test it

#

okay i fixed it

#
local replicatedStorage = game.ReplicatedStorage

local roundTime = 120
local button = replicatedStorage:FindFirstChild("button")

local function rng()
    local rngValue = math.random(17, 17) -- Always spawns for now
    if rngValue == 17 and button then
        local buttonClone = button:Clone()
        buttonClone.Parent = workspace
        local randomPosition = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
        buttonClone:PivotTo(CFrame.new(randomPosition))

        task.wait(0.1)

        local main = buttonClone:FindFirstChild("main")
        if main then
            local press = main:FindFirstChild("ClickDetector")
            if press then
                press.MouseClick:Connect(function()
                    local chance = math.random(1, 2)
                    if chance == 2 then
                        local part = Instance.new("Part")
                        part.Position = Vector3.new(math.random(-52, 52), 3, math.random(-52, 52))
                        part.Anchored = false
                        part.Parent = workspace
                    else
                    end
                end)
            end
        end
    end        
end

for i = 1, roundTime do 
    rng()
    task.wait(1)
end
#

try this

#

spawns the button with 100% and then 50% to spawn a part if u press the button

knotty cloak
#

now it doesnt spawn

#

wait nvm

mystic sun
#

what doesnt spawn

knotty cloak
#

holy shit i got 50% 6 times in a row

#

yeah it works

mystic sun
#

nice

knotty cloak
#

my god thank you dude 🙏

#

what did you change?

mystic sun
#

not much, but moved the local chance = math.random inside the press.MouseClick:Connect(function()

knotty cloak
#

thats it?

#

oooooh

#

omg it makes so much sense

mystic sun
#

u had the logic in wrong place yea

knotty cloak
#

thank you dude 🙏

mystic sun
#

np

#

took kinda time to figure it out, been only learning scripting for like 3 days now

knotty cloak
#

easiest part of the project done

knotty cloak
mystic sun
#

that was easy?

#

oh hell na

#

so simple mistake

knotty cloak
mystic sun
#

took so long to find

#

cant even imagine the next steps

knotty cloak
#

i gotta learn how to work on UI aswell

#

and thats my achiles heel

#

anyway thanks again and buh bye!