Hello, im currently making a spawn vehicle system, and to start that, it first came an idea on my mind of first creating a script to set the vehicle spawn point to the closest spawn point that CANT be seen by a player, these “spawn points” are parts that i put all around the map as places the vehicle could spawn.
Its basically the GTA ONLINE system of calling your vehicle, and i thought it was fancy doing that way.
The problem is, it detects the closest part, but it doesnt detect if you cant see it or not, so the car would spawn right in front of you, which is not what i would like. I tried implement the WorldToViewportPoint, so it ignores guiInset and stuff, but i have failed, i dont really know how i would implement this.
server script (which does the whole work)
local VehicleSpawns = workspace.Map.VehicleSpawnPoints
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Events:WaitForChild("SetVehicleSpawnPoint")
game.Players.PlayerAdded:Connect(function(Player)
Player.Changed:Connect(function(Prop)
if Prop == "Character" then
repeat wait(.1) until Player.Character
local Character = Player.Character
--
while true do
wait()
local Closest
local PlayerPosition = Character.PrimaryPart.Position
--
if unpack(VehicleSpawns:GetChildren()) ~= nil then
for i,v in pairs(VehicleSpawns:GetChildren()) do
if Closest == nil then
Closest = v
else
if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
Closest = v
end
end
end
end
--
if Closest ~= nil then
for i,v in pairs(VehicleSpawns:GetChildren()) do
if v == Closest then
local _, visibleClosest = workspace.CurrentCamera:WorldToViewportPoint(Closest.Position)
if not visibleClosest then
v.BrickColor = BrickColor.Green()
event:FireClient(Player, Closest)
end
else
v.BrickColor = BrickColor.Red()
end
end
end
end
end
end)
end)
local script (which sets the object value on the replicated storage)
local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Events:WaitForChild("SetVehicleSpawnPoint")
local avaliableSpawn = replicatedStorage.Values:WaitForChild("AvaliableVehicleSpawnPoint")
event.OnClientEvent:Connect(function(closestSpawn)
avaliableSpawn.Value = closestSpawn
end)