write luau code that will run in server, if some player chatted /spawn it will respawn him instantly, and then after respawned it will spawn 4 parts floating with hrp + Y vector3 5 coord smoothly like pets in some pet simulator game. The parts will change colors like rainbow smoothly and orbit. There will be a command "/change" and there will be 2 modes, first mode will be normal orbit, the second mode is the parts will still be hrp + Y vector3 5 coord is the parts will look where im looking ( look vector ).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is one complete example. In this implementation, when a player chats “/spawn” the server immediately resets (respawns) their character and then spawns four “pet‐parts” that hover in a circle above the character’s HumanoidRootPart (HRP). They change color along a rainbow (using an HSV cycle) and orbit around in mode 1. When the player chats “/change” the mode toggles so that in mode 2 the pets line up in front of the character (using the HRP’s LookVector) rather than orbiting. (You can further tweak the positions, speeds, and colors to your liking.)
Place this script in a Script inside ServerScriptService:
lua
--[[
This script listens for player chat commands.
When a player sends "/spawn" the server will
respawn their character and create four pet parts.
The four parts change colors smoothly and update each frame:
• Mode 1: They orbit (a circle around HRP at a distance of 5 with Y offset = 5).
• Mode 2: They form a formation in front of the player (using the HRP’s LookVector).
Chat "/change" to toggle between the two modes.
--]]
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- We'll store each player's pet data here
local petData = {} -- key: player, value: { mode = number, parts = { [1]=Part, ... } }
-- A helper to create the 4 "pet" parts
local function spawnPetsFor(player)
-- check that there is a character with a HRP:
if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then
return
end
-- if they already have pets, remove them
if petData[player] then
for _, part in ipairs(petData[player].parts) do
if part and part.Parent then
part:Destroy()
end
end
end
-- Create a new petData container for the player
petData[player] = {
-- use mode 1 (orbit) by default
mode = petData[player] and petData[player].mode or 1,
parts = {}
}
-- Create 4 parts
for i = 1, 4 do
local part = Instance.new("Part")
part.Name = "PetPart_" .. i
part.Shape = Enum.PartType.Ball
part.Material = Enum.Material.Neon
part.Size = Vector3.new(1, 1, 1)
part.Anchored = true
part.CanCollide = false
part.Parent = workspace
table.insert(petData[player].parts, part)
end
end
-- Listen for new players joining the game
Players.PlayerAdded:Connect(function(player)
-- Listen for that player's chat; you can use :Chatted on the Player instance.
player.Chatted:Connect(function(msg)
if msg == "/spawn" then
-- Force a respawn – this resets the character.
player:LoadCharacter()
-- Wait until the new character loads
player.CharacterAdded:Wait()
-- Create the pets (after a short wait so HRP exists)
wait(0.5)
spawnPetsFor(player)
elseif string.sub(msg, 1, 7) == "/change" then
-- Toggle the pet mode if pets have been created already.
if petData[player] then
if petData[player].mode == 1 then
petData[player].mode = 2
else
petData[player].mode = 1
end
end
end
end)
-- If the player's character respawns naturally, you might want to re-create the pets.
player.CharacterAdded:Connect(function(character)
-- If the player already had pets, re-spawn them.
if petData[player] then
wait(0.5) -- let the HumanoidRootPart load
spawnPetsFor(player)
end
end)
end)
-- Every frame, update the pet parts positions and colors.
RunService.Heartbeat:Connect(function(dt)
local t = tick() -- current time, used to compute smooth motion
for player, data in pairs(petData) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local hrp = player.Character.HumanoidRootPart
for i, part in ipairs(data.parts) do
local targetPos = nil
if data.mode == 1 then
-- Mode 1: Orbit mode.
-- Compute a circular orbit around the HRP.
local angle = t + (i - 1) * (2 * math.pi / 4)
targetPos = hrp.Position + Vector3.new(math.cos(angle) * 5, 5, math.sin(angle) * 5)
else
-- Mode 2: Formation in front of the player.
-- We use the HRP's LookVector to determine the forward direction.
local lookVec = hrp.CFrame.LookVector
local rightVec = hrp.CFrame.RightVector
-- We set up a formation relative to the HRP
if i == 1 then
targetPos = hrp.Position + lookVec * 5 + Vector3.new(0, 5, 0)
elseif i == 2 then
targetPos = hrp.Position + lookVec * 5 + rightVec * 2 + Vector3.new(0, 5, 0)
elseif i == 3 then
targetPos = hrp.Position + lookVec * 5 - rightVec * 2 + Vector3.new(0, 5, 0)
elseif i == 4 then
targetPos = hrp.Position + lookVec * 5 + rightVec * 4 + Vector3.new(0, 5, 0)
end
end
-- Smoothly move the pet part toward its target position.
local currentPos = part.Position
local newPos = currentPos:Lerp(targetPos, 0.2)
part.CFrame = CFrame.new(newPos)
-- Change the color gradually like a rainbow:
-- We use the HSV color wheel; each pet gets a slight offset.
local hue = math.fmod((t / 5 + (i - 1) / 4), 1)
part.Color = Color3.fromHSV(hue, 1, 1)
end
end
end
end)
-- Clean up pet parts when a player leaves
Players.PlayerRemoving:Connect(function(player)
if petData[player] then
for _, part in ipairs(petData[player].parts) do
if part and part.Parent then
part:Destroy()
end
end
petData[player] = nil
end
end)
How It Works
Chat Commands:
When a player sends /spawn the script calls player:LoadCharacter(), waits for the character to be added, then spawns four pet parts.
When a player sends /change the script toggles between mode 1 (orbiting the HRP) and mode 2 (lined-up formation in front of the player).
Pet Behavior (RunService Heartbeat Loop):
In mode 1 the parts’ positions are computed using a circular (orbit) formula using math.cos/math.sin with a time-based angle.
In mode 2 the parts are placed in fixed offsets in front of the player using the HRP’s LookVector and RightVector.
The parts’ positions are updated smoothly using Lerp so that movement appears fluid.
The parts’ colors change over time by converting a time-based hue (with a slight offset per pet) to a Color3 value via Color3.fromHSV.
Cleanup:
When a player leaves the game, any pet parts they spawned are cleaned up.
This complete example should work in Roblox using Luau on the server. Feel free to adjust the offsets, speeds, or appearance as needed for your game.