Randomly spawning objects

Hello,

I am trying to make a basic survival game. I know this is a very competitive genre, but I am still going to try. I have run into a bit of an issue.

I am using the recently published “Infinite Terrain Plugin” plugin for map generation (It should be noted that the plugin renders terrain client-side). Now, as goes for any survival game, I need a way of gathering stuff.

I have no idea how, to even begin spawning trees randomly. I have been looking for an answer for weeks (with no results).

Here comes my question: Can this be done?

These should be noted:
.

  • Objects should spawn server-side
  • Cutting down a tree, will remove it for all clients.
  • Is not hard on resources
  • System does not have a big delay

.
Thanks in advance,
greipster

Couldn’t you just spawn them in a random position and then fire a remote function to the client to get the correct Y position in relation to the terrain?

1 Like

How do you suppose I get the Y from the client?

  • I call the client for the Y
  • Client gets the position and ?

Well I’d start the Y position out pretty high so that it wouldn’t be inside of any terrain. Then on the client I’d shoot a ray down untill it hits the terrain and then move the tree down the length of the ray. If you’re not quite sure how to do that I can send a few bits of code for you.

I was just reasearching Raycasting, but I think, that I’d probably get it working faster with some guidance.

It would be better for you if you learned it yourself. You will remember it better that way. However, if you get stuck I’ll be glad to help.

1 Like

edit: You-Tube could also help

1 Like

You could have the server generate it out to a certain distance at startup which would require an initial load maybe

2 Likes

@JamminRedPandaMan also, I might seem stupid, but, which client do I send the function to?

1 Like

Well if you want the trees to load when the player joins you should probably do something like this

game.Players.PlayerAdded:Connect(function(player)
     -- you can send it to the "player" parameter like this
     remoteEvent:FireClient(player)
end)

Make sure you load the tree to a random position on the server first though.

1 Like

Or if you wanted to load the trees at a certain time you could use :FireAllClients()

2 Likes

Hold on I just relized this would run every time a player joins the game. Is this a single player survival game?

There is a centralized lobby, but the games themselves are ran on “private” servers, if thats what you mean.

And also, I’m very insecure about this code:

Server

local event = game:GetService("ReplicatedStorage").Events.positionTrees

game:GetService("Players").PlayerAdded:Connect(function(plr)
	
	event:FireClient(plr)
	
end)

Client

local event = game:GetService("ReplicatedStorage").Events.positionTrees
local trees = {}

event.OnClientEvent:Connect(function()
	
	for i, v in pairs(game:GetService("Workspace").Trees:GetChildren()) do
		
		if v and v.Name == "Tree" and not (table.find(trees, v)) then
			
			table.insert(trees, v)
			local pos = v.Position
			
			
			--                  [RAYCAST STUFF]							--
			local rayOrigin = pos
			local rayDirection = Vector3.new(0, -210, 0)
			
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {v}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			
			if raycastResult then
				
				local targetMaterial = raycastResult.Material
				local targetPos = raycastResult.Position
				local targetInstance = raycastResult.Instance
				
				if targetInstance == game:GetService("Workspace").Terrain and targetMaterial ~= Enum.Material.Water then
					
					-- position the tree
					
				end
				
			end
			--		--			--			--				--			--
			
		end
		
	end
	
end)

How would I go about spawning the around the map without creating much lag @JamminRedPandaMan ?

What in the world did I send you?! That is not how I raycast :joy:. Let me get some code I’ll get back to you soon.

1 Like

I mean it works? Is there a more efficient way?

You tested it and it works? (30)

This works kinda

if not game.Loaded then game.Loaded:Wait() end
local event = game:GetService("ReplicatedStorage").Events.positionTrees
local trees = {}

event.OnClientEvent:Connect(function()
	
	for i, v in pairs(game:GetService("Workspace").Trees:GetChildren()) do
		
		if v and v.Name == "Tree" and not (table.find(trees, v)) then
			
			table.insert(trees, v)
			local pos = v.Position
			
			--                  [RAYCAST STUFF]							--
			local rayOrigin = pos
			local rayDirection = Vector3.new(0, -210, 0)
			
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {v}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			
			local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
			
			if raycastResult then
				
				local targetMaterial = raycastResult.Material
				local targetPos = raycastResult.Position
				local targetInstance = raycastResult.Instance
				
				if targetInstance == game:GetService("Workspace").Terrain and targetMaterial ~= Enum.Material.Water then
					
					v.Position = targetPos + Vector3.new(0, 7, 0)
					
				end
				
			end
			--		--			--			--				--			--
			
		end
		
	end
	
end)

The problem seems to be that it only moves the first few trees.

Any errors? (30))))))))))))))))