Hello.
I am trying to make a beacon effect for a game I am making, which clones a beacon into the workspace from serverstorage, and should spawn at a random point through the map.
The points were made from parts, in which I take the position of the beam and equal that to the part. I also want it to start after an event is fired, and stops when another event is fired. This should be able to be done repeatedly.
This is the script that I have currently:
game.ReplicatedStorage.Event:Connect(function()
for i, v in pairs(game.Workspace.Points:GetDescendants()) do
if v:IsA("Part") then
for i = 1,v do
wait(math.random(0.1,1))
local a = game.ServerStorage.Beam:Clone()
a.Parent = workspace
a.Position = v.Position
end
if game.Lighting.ClockTime == 7.2 then
break
end
end
end
end)
I put clocktime in replacement of the “Event1” mechanism which should stop the beacon firing.
I am a beginner scripter, with experience in only basic stuff. Feedback is appreciated.
I intend it to clone beacons at random points, to the points in the “Points” folder. I would use If then statements but I have 18 points in the folder and that would be quite extensive.
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- This is the main thing you need for the Randomizer, Random.new() takes "seed" too
-- But you can skip the seed
local RNG = Random.new()
-- This the beacon, The one we want to spawn at random location
local beacon = ServerStorage.Beacon
-- This the points folder, All of the points are in this folder
local points = workspace.Points
-- This is the last beacon we spawned, So we can clean it up after we generate another one
local lastBeacon = nil
-- Now we have the function that we call for creating the beacon at random position
local function spawnBeaconAtRandomPoint()
-- Check if lastBeacon isn't nil, So we can destroy
if lastBeacon ~= nil then
lastBeacon:Destroy()
end
-- Now we do the random point part
-- This is a array with bunch of instances (our points)
local pointsChildren: {[number]: Instance} = points:GetChildren()
-- Since :GetChildren() returns an array, We can use #array to get how many values are in the array.
-- So for example we have 8 points in the points folder, This value will be 8.
local totalPoints = #pointsChildren
-- Select the random point using the 2 values we have
-- So RNG:NextInteger() will return a number between 1 and 8, And since our points table look like this
-- {[1]: Instance, [2]: Instance, [number]: Instance} we can use this "number" as a key to get random instance
local randomPoint = pointsChildren[RNG:NextInteger(1, totalPoints)]
-- We set the "lastBeacon" to the new beacon so we can destroy it later
-- We parent it to workspace after setting the pivot to randomPoint pivot
lastBeacon = beacon:Clone()
lastBeacon:PivotTo(randomPoint:GetPivot())
lastBeacon.Parent = workspace
end
-- Now we connect the events
ReplicatedStorage.Bind.Event:Connect(spawnBeaconAtRandomPoint)
I wrote the explanation as much as i could, This is the best i can do for you.
You need to learn how to use Random library which is built in Roblox
Thank you, however you sorta misinterpreted what I’m going for. You go the points right, however the beams should repeatedly clone, leaving multiple instances at the random points. They delete automatically, as I made a script put inside of the beam for that. It’s very difficult to describe what I’m trying to aim for, but It’s sort of like lightning. Very distant beams that appear at specific points, but in a random order and timing.
I misinterpreted. There’s no true “shape” As its simple roughly in a circle. Distances between the points are INCREDIBLY far from spawn, but the beacon can still be seen from spawn. They go in a very crude horseshoe shape.
The shape of the region induced by how the points are spread out.
Is there any other constraints? e.g. beams should spawn in locations which minimize the sum of every player’s distance to modify fairness, is picking each location equally probability or should it be uniform with respect to the density induced by how far or close some of the locations are to each other, or should locations close to other locations be prioritized less so locations that are not near other points will be less likely to never be picked?
Also to answer your questions, there are no other constraints. There’s no density, as I made most of the points in a rough linear fashion to make it more natural. They exist behind a mountain mesh, which stretches a bit far from spawn. The distance shouldn’t be an issue, however. It should just be timed randomly, like it would just pick a random point and then clone the beam into the workspace at that point’s position. It would wait a randomized amount of time as shown, and then it will pick another random point and then clone there.
I’ve attempted to implement the code into my game and fit it in as much as possible. HOWEVER, It’s not cloning the beams into workspace, and are not appearing on the explorer, nor giving me errors after I had already solved the prior errors to adapt the code for my game.