How can i improve this? I want to spawn a vehicle on selected position. Just one forever.
Problem: sometimes there is nothing spawned.
Server:
alGespawnedTitanic = false
RegisterServerEvent('DP_Wrakken:SpawnTitanic')
AddEventHandler('DP_Wrakken:SpawnTitanic', function()
if alGespawnedTitanic == false then
local model = tostring("titanic2")
local s = tonumber(source)
if s > 0 then
local player = CPlayer(s)
local titanic = Vehicle(true, model, -1872.7, -1273.22, -13.5, 50.0, true)
titanic:WaitForExistence()
titanic:Freeze(true)
alGespawnedTitanic = true
end
else
end
end)
Client:
Citizen.CreateThread(function() -- goed
lul = true
while true do
Citizen.Wait(0)
if lul == true then
if GetDistanceBetweenCoords(-1872.7, -1273.22, -2.4, GetEntityCoords(PlayerPedId()).x, GetEntityCoords(PlayerPedId()).y, GetEntityCoords(PlayerPedId()).z, true) < 1500 then
TriggerServerEvent('DP_Wrakken:SpawnTitanic')
lul = false
end
end
end
end)
I did client sided spawn only in my first editions. Client sided will cause to spawn the vechile multiple times. Classes resource is helping me spawning them only once more easy.
I don’t really understand this problem, probably when you spawn the vehicle there isn’t any player on the scope of the vehicle. Try to reduce the distance to spawn to 500 units not 1500.
The :Freeze() method doesn’t need a true to be passed, it only accept a number.
For example:
-- vehicle spawned
titanic:Freeze(500) -- this means will be unfrozen after 500 ms
if titanic.freezed then -- there's a variable inside to check if it's frozen or not
titanic:Freeze() -- if .freezed was true, only calling this method again will unfreeze it
end
P.S.:
You could save the vehicle spawned server-side as a global var, so when a player tries to spawn the vehicle, you can check if the vehicle:Exists() to spawn a new one or not.