Spawning script

Hello, back again with another scripting problem.
I’m a beginner at scripting, keep that in mind lol.
This is a script to spawn these objects that fly through the air(dont worry about the air part) but I tested it out and as I expected, it didn’t work. No errors have shown up, it just doesn’t spawn the parts.

I don’t even know if I was supposed to use run service honestly but someone said its better than other loops.

local tumble = Instance.new("MeshPart")
local force = Instance.new("BodyForce")
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	wait(math.random(5, 6)) --thats set to really low wait time so I could test it---
	tumble.Position = Vector3.new(math.random(2342, -2342), 159.941, -3017.581)
	tumble.MeshId = "rbxassetid://7545230286"
	tumble.Parent = workspace
	tumble.TextureID = "rbxassetid://6879551990"
	tumble.Material = Enum.Material.ForceField
	tumble.Size = Vector3.new(20.218, 19.503, 16.909)
	force.Parent = tumble
	force.Force = Vector3.new(0,1,10000)
	wait(math.random(10, 15))
	tumble.Transparency = 0.8
	force.Force = Vector3.new(0,0,0)
	tumble:Destroy()
end)

Thanks!

1 Like

Can you give me this info:

1.- Where is located?

1 Like

is the output set correctly?
because
RunService.RenderStepped
only works in a localscript

1 Like

You can also use it in moduleScript but only if is required by the client side.

1 Like

Workspace in an event folder. Its a local script for all those wondering, should it not be a local script I’m guessing? Again. I’m awful at coding but I’m getting better so remember.

1 Like

The localscripts dont run in the workspace.
You can put it in StarterPlayer.StarterPlayerScripts

1 Like

Yeah I don’t understand module scripts so I stayed away from that, I guess I’ll switch to a non local script but is there a loop I could use? I use while true do sometimes but I don’t know if thats ideal.

1 Like

Okay so, you did spawn the part, you just destroyed it and tried to set the position on the destroyed part
also, you don’t need that wait when using rendered stepped if you’re trying to spawn it every frame

Put that local tumble inside the runservice function
your script should look like this

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()

local tumble = Instance.new("MeshPart")
local force = Instance.new("BodyForce")
	tumble.Position = Vector3.new(math.random(2342, -2342), 159.941, -3017.581)
	tumble.MeshId = "rbxassetid://7545230286"
	tumble.Parent = workspace
	tumble.TextureID = "rbxassetid://6879551990"
	tumble.Material = Enum.Material.ForceField
	tumble.Size = Vector3.new(20.218, 19.503, 16.909)
	force.Parent = tumble
	force.Force = Vector3.new(0,1,10000)
	wait(math.random(10, 15))
	tumble.Transparency = 0.8
	force.Force = Vector3.new(0,0,0)
	tumble:Destroy()
end)
1 Like
  1. If this is a server script, don’t use renderstepped. Use while instead.
  2. You are not cloning the original part, so there will only ever be one moving around for a bit until it is destroyed
  3. It is not anchored, so it probably despawned.
1 Like

yeah, don’t use renderedstepped on a sever because it isn’t rendering frames, renderedstepped is only for clients, and should be used for visual effects only

1 Like

If you want to update a part’s physical settings before physics and calculated (i.e fly-scripts and their bodyvelocitys) then use stepped, if you want to update a parts physical settings after physics are calculated then use heartbeat

1 Like

Theres a lot of replies so I’ll just make this main comment.
I’m making loose objects roll over the ground in one direction, after a certain amount of time they get destroyed so it doesnt cause build up.
Also, it gets destroyed after waiting a bit, thats what I need.
I would like to spawn multiple but I don’t know how.

Questions:
How do I clone an original from rep-storage? I tried but I couldnt get it to work…
Do I need to use a normal script instead of local? It just needs to spawn objects that roll and then destroy, nothing specific for local players.
If I do switch it, do I use the “while true do” code to loop it?

1 Like

I would like to use it before physics start, but how do I do this without a local script?

1 Like

if you want all clients to see it, then put it in a normal script
And, like I said, don’t use RenderedStepped if you are switching to a server script

1 Like

Just swap out renderstepped it stepped

local RunService = game:GetService("RunService")

RunService.Stepped:Connect(function()

local tumble = Instance.new("MeshPart")
local force = Instance.new("BodyForce")
	tumble.Position = Vector3.new(math.random(2342, -2342), 159.941, -3017.581)
	tumble.MeshId = "rbxassetid://7545230286"
	tumble.Parent = workspace
	tumble.TextureID = "rbxassetid://6879551990"
	tumble.Material = Enum.Material.ForceField
	tumble.Size = Vector3.new(20.218, 19.503, 16.909)
	force.Parent = tumble
	force.Force = Vector3.new(0,1,10000)
	wait(math.random(10, 15))
	tumble.Transparency = 0.8
	force.Force = Vector3.new(0,0,0)
	tumble:Destroy()
end)
1 Like

this should work on all scripts if you use stepped, but the most performant on clients would be RenderedStepped

Let me know if you’re trying to do this at a certain rate (i,e. 15 parts per-second)

1 Like

I’m trying to make parts spawn at 10-20 seconds, and then they disappear after 15-20 seconds. Also it says the math.random function inside of the position setting is invalid because “intervals empty” do you know why that is?

local WeedMaker = {}
local OriginalWeed = PathToOriginalWeed
-- Anchor it!
function WeedMaker.NewWeed(StartingPosition,Force)
   local NewWeed = OriginalWeed:Clone()
   NewWeed.Anchored = false
  NewWeed.Parent = workspace
   NewWeed.Position = StartingPosition
   local Force = Instance.new("BodyForce")
  Force.Parent = NewWeed
  Force.Force = Vector3.new(0,1,1000) -- I think this might not work, I don't know for sure, I dont really use bodyforces at all.
  wait(10)
  NewWeed:Destroy()
end
return WeedMaker
-- call WeedMaker.NewWeed()

yeah, you either set the max too high or set the minimum higher than the max

when using math.random() it uses the first number as the minum and the second as the maximum
if you set the minimum higher than the maximum, then you get an error like that

by the way, you can get decimals anymore from math.random()
You have to set that stuff yourself.
ya know what, i’ll make a good example of what to do ok? give me a second