How can I make it spawn in another position?

well, what I want to do is that if it is the first InnateDomain and another player makes another one appear, it does not appear in the same position that the other InnateDomain is already in. I mean, I want them to appear separated from the initial position because then 1 player spawns the innate and another They would also be in the same area and I don’t want that.

local playerName = plr.Name
					local innateDomainClone = RS.adminwarning.WARNING.InnateDomain:Clone()
					innateDomainClone.Name = "InnateDomain " .. playerName .. "'s"
					innateDomainClone.Parent = workspace

Try my script:

local RS = game:GetService("ReplicatedStorage") -- Ensure RS is set to the correct service

local function getNewPosition(basePosition, distanceBetweenDomains, maxAttempts)
    local attempts = 0

    while attempts < maxAttempts do
        local xOffset = math.random(-20, 20) -- Randomize X position
        local zOffset = math.random(-20, 20) -- Randomize Z position
        local newPosition = basePosition + Vector3.new(xOffset, 0, zOffset)

        local canSpawn = true

        -- Check if this new position is too close to existing InnateDomains
        for _, existingDomain in pairs(workspace:GetChildren()) do
            if existingDomain:IsA("Model") and existingDomain.Name:match("InnateDomain") then
                if existingDomain:FindFirstChild("PrimaryPart") then
                    local distance = (existingDomain.PrimaryPart.Position - newPosition).Magnitude
                    if distance < distanceBetweenDomains then
                        canSpawn = false
                        break
                    end
                end
            end
        end

        if canSpawn then
            return newPosition
        end

        attempts = attempts + 1
    end

    -- Fallback to base position if no valid position found after attempts
    return basePosition
end

local function spawnInnateDomain(plr)
    local playerName = plr.Name
    local innateDomainTemplate = RS:WaitForChild("adminwarning"):WaitForChild("WARNING"):WaitForChild("InnateDomain")
    local innateDomainClone = innateDomainTemplate:Clone()
    innateDomainClone.Name = "InnateDomain " .. playerName .. "'s"
    innateDomainClone.Parent = workspace

    -- Define your parameters
    local basePosition = Vector3.new(0, 0, 0) -- Change this to your desired base spawn position
    local distanceBetweenDomains = 10 -- Minimum distance between each InnateDomain
    local maxAttempts = 100 -- Limit the number of attempts to avoid infinite loops

    -- Set the position of the new InnateDomain
    local newPosition = getNewPosition(basePosition, distanceBetweenDomains, maxAttempts)
    innateDomainClone:SetPrimaryPartCFrame(CFrame.new(newPosition))
end

-- Example usage: Call spawnInnateDomain(player) where player is a valid player object
1 Like

it says on the console error on this line local player Name = plr.Name attempt index nil with ‘Name’ when i try to spawn it

1 Like

and I made yours but it shows that it is unable to CoordinateFrame and I did put primarypart even though I don’t know if it’s because I didn’t put welds on it

You can just randomize the position using math.random:
local playerName = plr.Name
local innateDomainClone =RS.adminwarning.WARNING.InnateDomain:Clone()
innateDomainClone.Name = "InnateDomain " … playerName … “'s”
innateDomainClone.Parent = workspace

If innatedomain is a Part:
innateDomainClone.Position = innateDomainClone.Position +
Vector3.new(math.random(0,20)-10,math.random(0,20)-10,math.random(0,20)-10)

But if innatedomain is a model use(Requires PrimaryPart and all parts welded to each other):
innateDomainClone:SetPrimaryPartCFrame(CFrame.new(innateDomainClone.PrimaryPart.Position+Vector3.new(math.random(0,20)-10,math.random(0,20)-10,math.random(0,20)-10)))

Yes, it helped me, thank you. It gave me a different position. I just need to customize the positions but from there everything is fine

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.