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

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

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

✓ Accepted answer

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

3 comments

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.

0

I thought your script didn't work, but they were spawning inside the baseplate because I forgot to lower it... XD

0

Haha glad to help

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