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.
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?
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.
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)
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)