write luau code that will run in server, if some player chatted /spawn it will respawn him, and then after respawned it will spawn 4 parts floating with hrp + Y vector3 5 coord smoothly like pets in some pet simulator game.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a Roblox Luau server-side script that listens for a player chatting /spawn, respawns the player, and then spawns 4 floating parts that smoothly follow the player's HumanoidRootPart (HRP) at a height offset of 5 studs, similar to pets in a pet simulator game.
How it works:
When a player chats /spawn, the script:
Respawns the player.
After the character loads, creates 4 parts.
Each part smoothly follows the player's HRP with a vertical offset of 5 studs.
The parts float around the player in a small circle, updating every frame.
Server Script (place in ServerScriptService):
lua
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- Configuration
local PET_COUNT = 4
local FLOAT_HEIGHT = 5
local FLOAT_RADIUS = 3
local FLOAT_SPEED = 2 -- radians per second
-- Function to create a pet part
local function createPetPart()
local part = Instance.new("Part")
part.Size = Vector3.new(1,1,1)
part.Shape = Enum.PartType.Ball
part.Anchored = true
part.CanCollide = false
part.Material = Enum.Material.Neon
part.Color = Color3.fromRGB(255, 170, 0) -- orange color
part.Name = "PetPart"
part.Parent = workspace
return part
end
-- Function to smoothly move a part to a target position
local function smoothMove(part, targetPos, deltaTime)
local currentPos = part.Position
local lerpAlpha = 0.1 -- smoothing factor (0-1)
local newPos = currentPos:Lerp(targetPos, lerpAlpha)
part.Position = newPos
end
-- Main function to spawn pets and make them float around the player
local function spawnPetsForCharacter(character)
local hrp = character:WaitForChild("HumanoidRootPart")
local pets = {}
-- Create pet parts
for i = 1, PET_COUNT do
local pet = createPetPart()
pets[i] = pet
end
-- Track time for floating animation
local startTime = tick()
-- Connect to RenderStepped to update pets every frame
local connection
connection = RunService.Heartbeat:Connect(function(deltaTime)
if not hrp or not hrp.Parent then
-- Character removed, cleanup pets and disconnect
for _, pet in pairs(pets) do
if pet and pet.Parent then
pet:Destroy()
end
end
connection:Disconnect()
return
end
local timeElapsed = tick() - startTime
local basePos = hrp.Position + Vector3.new(0, FLOAT_HEIGHT, 0)
for i, pet in ipairs(pets) do
-- Calculate circular floating position around HRP
local angle = (timeElapsed * FLOAT_SPEED) + (2 * math.pi * (i-1) / PET_COUNT)
local offset = Vector3.new(math.cos(angle), 0, math.sin(angle)) * FLOAT_RADIUS
local targetPos = basePos + offset
-- Smoothly move pet to target position
smoothMove(pet, targetPos, deltaTime)
end
end)
end
-- Listen for player chats
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message:lower() == "/spawn" then
-- Respawn player
if player.Character then
player.Character:BreakJoints()
end
-- Wait for character to load
player.CharacterAdded:Wait()
-- Spawn pets after character respawn
spawnPetsForCharacter(player.Character)
end
end)
end)
Explanation:
When a player types /spawn, their character is respawned by breaking joints.
After the new character loads, 4 neon ball parts are created.
These parts float in a circle around the player's HRP at 5 studs height.
The parts smoothly interpolate their position every frame for smooth movement.
If the character dies or is removed, the pets are cleaned up.
If you want the pets to persist or have more complex behavior (like following at different distances or reacting to player movement), you can expand on this base.