The damn cows spawn in the water all the time, and I want that to change
-- Configuration
local respawnIntervalMin = 10 -- Minimum respawn interval in seconds
local respawnIntervalMax = 30 -- Maximum respawn interval in seconds
-- Function to respawn an NPC at a random location
local function respawnNPC()
-- Get a random position within the workspace
local randomPosition = Vector3.new(math.random(200, 200), 5, math.random(200, 200))
-- Move the NPC to the random position
local newNPC = game.ServerStorage.NPC:Clone()
newNPC:SetPrimaryPartCFrame(CFrame.new(randomPosition))
newNPC.Parent = workspace
end
-- Function to create and respawn NPCs at random intervals
local function startRandomNPCRespawns()
while true do
wait(math.random(respawnIntervalMin, respawnIntervalMax))
respawnNPC()
end
end
-- Start the coroutine to respawn NPCs at random intervals
startRandomNPCRespawns()
First off your math.random is between 200 and 200 so it’ll always be 200.
Also, you could send a raycast down to see if it hits water and if it does recalculate the position.
local function getRandomPosition()
local randomPosition = Vector3.new(math.random(0, 200), 5, math.random(0, 200))
local raycastParams = RaycastParams.new()
raycastParams.IgnoreWater = false
local raycastResult = workspace:Raycast(randomPosition, Vector3.new(0, -1000, 0))
if raycastResult then
if raycastResult.Material == Enum.Material.Water then
return getRandomPosition()
else
return randomPosition
end
else
return randomPosition
end
end
local function respawnNPC()
local randomPosition = getRandomPosition()
-- Move the NPC to the random position
local newNPC = game.ServerStorage.NPC:Clone()
newNPC:SetPrimaryPartCFrame(CFrame.new(randomPosition))
newNPC.Parent = workspace
end
Are there any errors? Also try printing out the random position to make sure it’s on the map.
local function respawnNPC()
local randomPosition = getRandomPosition()
print(randomPosition)
-- Move the NPC to the random position
local newNPC = game.ServerStorage.NPC:Clone()
newNPC:SetPrimaryPartCFrame(CFrame.new(randomPosition))
newNPC.Parent = workspace
end
Oh right, use :MoveTo instead. Also do that after you parent it.
local function respawnNPC()
local randomPosition = getRandomPosition()
-- Move the NPC to the random position
local newNPC: Model = game.ServerStorage:WaitForChild("NPC"):Clone()
newNPC.Parent = workspace
newNPC:MoveTo(randomPosition)
end