I have a script that moves around an npc in a ring and starts rounds when the player touches the start platform. I recently added the npc move around part and tried it in a few different loops, and it works but it breaks the rest of the script and the player can’t start rounds / it doesn’t detect when the player touches the start platform.
Code:
local platform = script.Parent
local ringTeleportPosition = script.Parent.Parent.PlayerSpawn.Position
local respawnPosition = Vector3.new(0, 0, 0)
local playerInRing = false
-- RemoteEvent to signal the start of the round
local roundEvent = Instance.new("RemoteEvent")
roundEvent.Name = "RoundEvent"
roundEvent.Parent = game.ReplicatedStorage
-- Function to manipulate the platform and beams
local function manipulatePlatformAndBeams(isVisible)
-- Set platform properties
platform.BrickColor = isVisible and BrickColor.new("Bright red") or BrickColor.new("Mid gray")
-- Find and manipulate beams inside the platform
for _, child in pairs(platform:GetChildren()) do
if child:IsA("Beam") then
child.Enabled = not isVisible
end
end
end
-- Get the Vector3 positions from the WalkToPoints instances
local Point1 = script.Parent.Parent.WalkToPoints["Point 1"].Position
local Point2 = script.Parent.Parent.WalkToPoints["Point 2"].Position
local Scarlet = script.Parent.Parent.Enemies["Scarlet \"Rainbow\" Underkill"]
local humanoid = Scarlet.Humanoid
-- Set a slower walk speed for Scarlet
humanoid.WalkSpeed = 1 -- You can adjust this value as needed
-- Function to play the animation
local function playAnimation(animationId)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. tostring(animationId)
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
return animTrack
end
while playerInRing == false do
-- Play the animation while moving to Point2
local animTrack1 = playAnimation(15700252729)
humanoid:MoveTo(Point2)
humanoid.MoveToFinished:Wait()
animTrack1:Stop() -- Stop the animation when movement is done
-- Play the animation while moving to Point1
local animTrack2 = playAnimation(15700252729)
humanoid:MoveTo(Point1)
humanoid.MoveToFinished:Wait()
animTrack2:Stop() -- Stop the animation when movement is done
end
local function resetEnemiesHealth()
local enemiesFolder = script.Parent.Parent.Enemies
if enemiesFolder then
for _, enemy in pairs(enemiesFolder:GetChildren()) do
if enemy:IsA("Model") and enemy:FindFirstChild("Humanoid") then
local enemyHumanoid = enemy:WaitForChild("Humanoid")
enemyHumanoid.Health = enemyHumanoid.MaxHealth
end
end
end
end
local function onPlatformTouched(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
if humanoid and player and not playerInRing then
playerInRing = true
local character = player.Character
if character then
-- Teleport player to the ring
character:SetPrimaryPartCFrame(CFrame.new(ringTeleportPosition))
-- Connect to player death event
character:WaitForChild("Humanoid").Died:Connect(function()
playerInRing = false
player:LoadCharacter()
-- Reset platform and beams when the player dies
manipulatePlatformAndBeams(false)
-- Reset enemies' health when the player dies
resetEnemiesHealth()
end)
-- Connect to NPC defeat event
local enemiesFolder = script.Parent.Parent.Enemies
if enemiesFolder then
local defeatedEnemies = 0
for _, enemy in pairs(enemiesFolder:GetChildren()) do
if enemy:IsA("Model") and enemy:FindFirstChild("Humanoid") then
local enemyHumanoid = enemy:WaitForChild("Humanoid")
enemyHumanoid.Died:Connect(function()
defeatedEnemies = defeatedEnemies + 1
if defeatedEnemies == #enemiesFolder:GetChildren() then
-- All enemies defeated, player wins
print(player.Name .. " wins!")
-- You can add more logic here for the player winning
playerInRing = false
player:LoadCharacter()
-- Reset platform and beams when the player wins
manipulatePlatformAndBeams(false)
end
end)
end
end
end
-- Set platform and beams properties when the player enters the ring
manipulatePlatformAndBeams(true)
-- Fire the RoundEvent to signal the start of the round
roundEvent:FireClient(player)
end
end
end
platform.Touched:Connect(onPlatformTouched)