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

Is there a way to make a position with two random variables and one constant?

Asked by 8 years ago

I know that there is a video tutorial on ROBLOX Wiki, but it will not fix this script. It is for an odd mining game that I am making, so that a dinosaur skull will spawn in random places, but it does not work. The skull blocks do not even appear. I know that this will probably be an easy fix for experienced scripters, so please take a few seconds and help me. Here is the game so far: https://www.roblox.com/games/544182596/No-name-yet-Open-during-construction

Here is the script:

local skull = script.Parent:Clone()
local xpos = math.random(-10,10)
local zpos = math.random(-10,10)

function skullspawn()
    skull.Parent = game.Workspace
    skull.Script:Destroy()
    skull.Position = Vector3.new(xpos, 0.02, zpos)
end

while true do
    skullspawn()
    wait(0.1)
end

1 answer

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

The problem is that xpos and zpos are also constant. The random function returns to you a number, in your between -10 and 10, so lines 2 and 3 is like saying xpos = -4 and ypos = 6, for example. You want to compute new random numbers each time the function is called. Also, skull is only ever cloned once, so the second time the function is run, 'Script' will no longer be a child of the skull. I am going to assume you want a brand new skull each time the 'skullspawn' function is called.

Solution:

function skullspawn()
    local x = math.random(-10, 10)
    local z = math.random(-10, 10)

    skull = script.Parent:Clone()
    skull.Script:Destroy()
    skull.Position = Vector3.new(x, 0.02, z)
    skull.Parent = game.Workspace
end

while true do
    skullspawn()
    wait(5) -- Spawn a new skull every 5 seconds
end
0
Thank you! I thought it was because they could collide and it was forcing strain on the system. But now that I think about it, your solution makes sense. Rupestrine 5 — 8y
0
I thought your script didn't work, but they were spawning inside the baseplate because I forgot to lower it... XD Rupestrine 5 — 8y
0
Haha glad to help BlackJPI 2658 — 8y
Ad

Answer this question