Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

local character = script.Parent local humanoid = character:WaitForChild("Humanoid")…

  1. User

    local character = script.Parent local humanoid = character:WaitForChild("Humanoid") local rootPart = character:WaitForChild("HumanoidRootPart") local head = character:WaitForChild("Head")

    local TweenService = game:GetService("TweenService") local PathfindingService = game:GetService("PathfindingService") local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local currentMode = 1 local modeChangeCooldown = 5 -- seconds local soundPlayChance = 0.2 -- 20% chance to play a sound each iteration local lifespanDuration = 10 -- seconds local spawnTime = tick()

    local function getRandomPosition() local randomAngle = math.random() * math.pi * 2 local randomDistance = math.random(10, 30) local newX = rootPart.Position.X + math.cos(randomAngle) * randomDistance local newZ = rootPart.Position.Z + math.sin(randomAngle) * randomDistance return Vector3.new(newX, rootPart.Position.Y, newZ) end

    local function getNearbyPlayers(maxDistance) local nearbyPlayers = {} for _, player in pairs(game.Players:GetPlayers()) do if player.Character and player.Character ~= character then local distance = (player.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude if distance <= maxDistance then table.insert(nearbyPlayers, player.Character) end end end return nearbyPlayers end

    local function getNearestPlayer() local nearestPlayer = nil local nearestDistance = math.huge

    for _, player in pairs(game.Players:GetPlayers()) do
    	if player.Character and player.Character ~= character then
    		local distance = (player.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
    		if distance < nearestDistance then
    			nearestDistance = distance
    			nearestPlayer = player.Character
    		end
    	end
    end
    
    return nearestPlayer
    

    end

    local function moveToPosition(targetPosition) local path = PathfindingService:CreatePath() path:ComputeAsync(rootPart.Position, targetPosition)

    if path.Status == Enum.PathStatus.Success then
    	local waypoints = path:GetWaypoints()
    
    	for i, waypoint in ipairs(waypoints) do
    		if waypoint.Action == Enum.PathWaypointAction.Jump then
    			humanoid.Jump = true
    		end
    		humanoid:MoveTo(waypoint.Position)
    		humanoid.MoveToFinished:Wait()
    	end
    end
    

    end

    local function tweenLookAt(targetPosition, duration) local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out) local startCFrame = rootPart.CFrame local endCFrame = CFrame.new(rootPart.Position, Vector3.new(targetPosition.X, rootPart.Position.Y, targetPosition.Z))

    local tween = TweenService:Create(rootPart, tweenInfo, {CFrame = endCFrame})
    tween:Play()
    tween.Completed:Wait()
    

    end

    local function lookAtRandomNearbyPlayer() local nearbyPlayers = getNearbyPlayers(50) -- Adjust the max distance as needed if #nearbyPlayers > 0 then local randomPlayer = nearbyPlayers[math.random(1, #nearbyPlayers)] tweenLookAt(randomPlayer.HumanoidRootPart.Position, 0.5) end end

    local function playRandomSound() if math.random() < soundPlayChance then local soundNumber = math.random(1, 4) local soundName = "Talk" .. soundNumber local sound = head:FindFirstChild(soundName) if sound and sound:IsA("Sound") then sound:Play() end end end

    local function stand() lookAtRandomNearbyPlayer() playRandomSound() wait(1) end

    local function followNearestPlayer() local nearestPlayer = getNearestPlayer() if nearestPlayer then local targetPosition = nearestPlayer.HumanoidRootPart.Position local randomOffset = Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)) moveToPosition(targetPosition + randomOffset) end playRandomSound() wait(1) end

    local function randomMovement() local targetPosition = getRandomPosition() local midPoint1 = rootPart.Position:Lerp(targetPosition, 0.33) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5)) local midPoint2 = rootPart.Position:Lerp(targetPosition, 0.66) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))

    moveToPosition(midPoint1)
    playRandomSound()
    wait(0.5)
    
    moveToPosition(midPoint2)
    playRandomSound()
    wait(0.5)
    
    moveToPosition(targetPosition)
    playRandomSound()
    wait(0.5)
    
    lookAtRandomNearbyPlayer()
    playRandomSound()
    
    wait(1 + math.random())
    

    end

    local function isPlayerNearMushroom(mushroom) local nearbyPlayers = game.Players:GetPlayers() for _, player in ipairs(nearbyPlayers) do if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then local distance = (player.Character.HumanoidRootPart.Position - mushroom.PrimaryPart.Position).Magnitude if distance <= 10 then -- Adjust this distance as needed return true end end end return false end

    local function transformIntoMushroom() local mushroomModel = ReplicatedStorage:FindFirstChild("Mushroom") if mushroomModel then local mushroom = mushroomModel:Clone() mushroom.Parent = workspace mushroom:SetPrimaryPartCFrame(rootPart.Parent["Left Leg"].CFrame)

    	character.Parent = ReplicatedStorage
    
    	while true do
    		if isPlayerNearMushroom(mushroom) then
    			mushroom:Destroy()
    			character.Parent = workspace
    			spawnTime = tick() -- Reset the spawn time
    			print("Character respawned. 10 seconds until next transformation.")
    			return
    		end
    		wait(1)
    	end
    else
    	warn("Mushroom model not found in ReplicatedStorage")
    end
    

    end

    -- Play death sound when the character dies humanoid.Died:Connect(function() local deathSound = head:FindFirstChild("Death") if deathSound and deathSound:IsA("Sound") then deathSound:Play() end end)

    local lastPrintTime = 0 local printInterval = 30 -- Print time left every 30 seconds

    while true do local currentTime = tick() local timeElapsed = currentTime - spawnTime local timeLeft = lifespanDuration - timeElapsed

    if timeLeft <= 0 then
    	print("Transforming into mushroom...")
    	transformIntoMushroom()
    else
    	if currentTime - lastPrintTime >= printInterval then
    		print(string.format("Time left before mushrooming: %.0f seconds", timeLeft))
    		lastPrintTime = currentTime
    	end
    
    	if currentMode == 1 then
    		stand()
    	elseif currentMode == 2 then
    		followNearestPlayer()
    	elseif currentMode == 3 then
    		randomMovement()
    	end
    
    	if math.random() < 0.2 then
    		currentMode = math.random(1, 3)
    		wait(modeChangeCooldown)
    	end
    end
    
    wait(0.1) -- Small wait to prevent excessive looping
    

    end

    can you enhance this ai system, and make it so that if it detects something in the stringvalue script.parent.Accesory.Cap.CanGrab, it will pause the ai until nothing is in the value.

  2. Assistant

    Certainly! I've enhanced your AI system to pause its actions whenever the CanGrab StringValue located at script.Parent.Accessory.Cap.CanGrab contains any value (i.e., is non-empty). The AI will remain paused until the CanGrab value becomes empty again.

    Here's the updated script with the necessary modifications:

    local character = script.Parent
    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")
    local head = character:WaitForChild("Head")
    
    local TweenService = game:GetService("TweenService")
    local PathfindingService = game:GetService("PathfindingService")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    
    local currentMode = 1
    local modeChangeCooldown = 5 -- seconds
    local soundPlayChance = 0.2 -- 20% chance to play a sound each iteration
    local lifespanDuration = 10 -- seconds
    local spawnTime = tick()
    
    local function getRandomPosition()
    	local randomAngle = math.random() * math.pi * 2
    	local randomDistance = math.random(10, 30)
    	local newX = rootPart.Position.X + math.cos(randomAngle) * randomDistance
    	local newZ = rootPart.Position.Z + math.sin(randomAngle) * randomDistance
    	return Vector3.new(newX, rootPart.Position.Y, newZ)
    end
    
    local function getNearbyPlayers(maxDistance)
    	local nearbyPlayers = {}
    	for _, player in pairs(game.Players:GetPlayers()) do
    		if player.Character and player.Character ~= character then
    			local distance = (player.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
    			if distance <= maxDistance then
    				table.insert(nearbyPlayers, player.Character)
    			end
    		end
    	end
    	return nearbyPlayers
    end
    
    local function getNearestPlayer()
    	local nearestPlayer = nil
    	local nearestDistance = math.huge
    
    	for _, player in pairs(game.Players:GetPlayers()) do
    		if player.Character and player.Character ~= character then
    			local distance = (player.Character.HumanoidRootPart.Position - rootPart.Position).Magnitude
    			if distance < nearestDistance then
    				nearestDistance = distance
    				nearestPlayer = player.Character
    			end
    		end
    	end
    
    	return nearestPlayer
    end
    
    local function moveToPosition(targetPosition)
    	local path = PathfindingService:CreatePath()
    	path:ComputeAsync(rootPart.Position, targetPosition)
    
    	if path.Status == Enum.PathStatus.Success then
    		local waypoints = path:GetWaypoints()
    
    		for i, waypoint in ipairs(waypoints) do
    			if waypoint.Action == Enum.PathWaypointAction.Jump then
    				humanoid.Jump = true
    			end
    			humanoid:MoveTo(waypoint.Position)
    			humanoid.MoveToFinished:Wait()
    		end
    	end
    end
    
    local function tweenLookAt(targetPosition, duration)
    	local tweenInfo = TweenInfo.new(duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
    	local startCFrame = rootPart.CFrame
    	local endCFrame = CFrame.new(rootPart.Position, Vector3.new(targetPosition.X, rootPart.Position.Y, targetPosition.Z))
    
    	local tween = TweenService:Create(rootPart, tweenInfo, {CFrame = endCFrame})
    	tween:Play()
    	tween.Completed:Wait()
    end
    
    local function lookAtRandomNearbyPlayer()
    	local nearbyPlayers = getNearbyPlayers(50) -- Adjust the max distance as needed
    	if #nearbyPlayers > 0 then
    		local randomPlayer = nearbyPlayers[math.random(1, #nearbyPlayers)]
    		tweenLookAt(randomPlayer.HumanoidRootPart.Position, 0.5)
    	end
    end
    
    local function playRandomSound()
    	if math.random() < soundPlayChance then
    		local soundNumber = math.random(1, 4)
    		local soundName = "Talk" .. soundNumber
    		local sound = head:FindFirstChild(soundName)
    		if sound and sound:IsA("Sound") then
    			sound:Play()
    		end
    	end
    end
    
    local function stand()
    	lookAtRandomNearbyPlayer()
    	playRandomSound()
    	wait(1)
    end
    
    local function followNearestPlayer()
    	local nearestPlayer = getNearestPlayer()
    	if nearestPlayer then
    		local targetPosition = nearestPlayer.HumanoidRootPart.Position
    		local randomOffset = Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
    		moveToPosition(targetPosition + randomOffset)
    	end
    	playRandomSound()
    	wait(1)
    end
    
    local function randomMovement()
    	local targetPosition = getRandomPosition()
    	local midPoint1 = rootPart.Position:Lerp(targetPosition, 0.33) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
    	local midPoint2 = rootPart.Position:Lerp(targetPosition, 0.66) + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
    
    	moveToPosition(midPoint1)
    	playRandomSound()
    	wait(0.5)
    
    	moveToPosition(midPoint2)
    	playRandomSound()
    	wait(0.5)
    
    	moveToPosition(targetPosition)
    	playRandomSound()
    	wait(0.5)
    
    	lookAtRandomNearbyPlayer()
    	playRandomSound()
    
    	wait(1 + math.random())
    end
    
    local function isPlayerNearMushroom(mushroom)
    	local nearbyPlayers = game.Players:GetPlayers()
    	for _, player in ipairs(nearbyPlayers) do
    		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
    			local distance = (player.Character.HumanoidRootPart.Position - mushroom.PrimaryPart.Position).Magnitude
    			if distance <= 10 then -- Adjust this distance as needed
    				return true
    			end
    		end
    	end
    	return false
    end
    
    local function transformIntoMushroom()
    	local mushroomModel = ReplicatedStorage:FindFirstChild("Mushroom")
    	if mushroomModel then
    		local mushroom = mushroomModel:Clone()
    		mushroom.Parent = workspace
    		mushroom:SetPrimaryPartCFrame(rootPart.Parent["Left Leg"].CFrame)
    
    		character.Parent = ReplicatedStorage
    
    		while true do
    			if isPlayerNearMushroom(mushroom) then
    				mushroom:Destroy()
    				character.Parent = workspace
    				spawnTime = tick() -- Reset the spawn time
    				print("Character respawned. 10 seconds until next transformation.")
    				return
    			end
    			wait(1)
    		end
    	else
    		warn("Mushroom model not found in ReplicatedStorage")
    	end
    end
    
    -- Play death sound when the character dies
    humanoid.Died:Connect(function()
    	local deathSound = head:FindFirstChild("Death")
    	if deathSound and deathSound:IsA("Sound") then
    		deathSound:Play()
    	end
    end)
    
    local lastPrintTime = 0
    local printInterval = 30 -- Print time left every 30 seconds
    
    -- Function to check if the AI should be paused
    local function shouldPauseAI()
    	local accessory = character:FindFirstChild("Accessory")
    	if accessory then
    		local cap = accessory:FindFirstChild("Cap")
    		if cap then
    			local canGrab = cap:FindFirstChild("CanGrab")
    			if canGrab and canGrab:IsA("StringValue") then
    				return canGrab.Value ~= ""
    			end
    		end
    	end
    	return false
    end
    
    while true do
    	-- Pause the AI if script.Parent.Accessory.Cap.CanGrab has a non-empty value
    	while shouldPauseAI() do
    		-- AI is paused; waiting until CanGrab is empty
    		wait(1) -- Check every second
    	end
    
    	local currentTime = tick()
    	local timeElapsed = currentTime - spawnTime
    	local timeLeft = lifespanDuration - timeElapsed
    
    	if timeLeft <= 0 then
    		print("Transforming into mushroom...")
    		transformIntoMushroom()
    	else
    		if currentTime - lastPrintTime >= printInterval then
    			print(string.format("Time left before mushrooming: %.0f seconds", timeLeft))
    			lastPrintTime = currentTime
    		end
    
    		if currentMode == 1 then
    			stand()
    		elseif currentMode == 2 then
    			followNearestPlayer()
    		elseif currentMode == 3 then
    			randomMovement()
    		end
    
    		if math.random() < 0.2 then
    			currentMode = math.random(1, 3)
    			wait(modeChangeCooldown)
    		end
    	end
    
    	wait(0.1) -- Small wait to prevent excessive looping
    end
    

    Explanation of Enhancements:

    1. Check for CanGrab Value:

      • A new function shouldPauseAI() has been added to determine if the AI should be paused.
      • This function checks if the CanGrab StringValue located at script.Parent.Accessory.Cap.CanGrab exists and whether it's non-empty.
      local function shouldPauseAI()
      	local accessory = character:FindFirstChild("Accessory")
      	if accessory then
      		local cap = accessory:FindFirstChild("Cap")
      		if cap then
      			local canGrab = cap:FindFirstChild("CanGrab")
      			if canGrab and canGrab:IsA("StringValue") then
      				return canGrab.Value ~= ""
      			end
      		end
      	end
      	return false
      end
      
    2. Pause the AI:

      • At the beginning of the main while true loop, the script now checks if the AI should be paused using the shouldPauseAI() function.
      • If CanGrab.Value is non-empty, the AI enters a pause state and repeatedly checks every second until CanGrab.Value becomes empty again.
      while true do
      	-- Pause the AI if script.Parent.Accessory.Cap.CanGrab has a non-empty value
      	while shouldPauseAI() do
      		-- AI is paused; waiting until CanGrab is empty
      		wait(1) -- Check every second
      	end
      
      	-- Rest of your AI logic follows...
      end
      
    3. Graceful Handling:

      • The shouldPauseAI() function safely checks for the existence of Accessory, Cap, and CanGrab to prevent any errors if they are not present.
      • The AI will continue its normal operations once CanGrab.Value is empty again.

    How It Works:

    • When CanGrab.Value Is Non-Empty:

      • The AI enters a paused state.
      • All AI actions are halted.
      • The script waits and checks every second to see if CanGrab.Value has become empty.
    • When CanGrab.Value Becomes Empty:

      • The AI resumes its normal behavior.
      • The script continues with the regular loop, performing actions based on currentMode and other logic.

    Notes:

    • Adjusting the Check Interval:

      • Currently, the script checks CanGrab.Value every second while paused (wait(1)).
      • You can adjust the interval of this check by changing the number inside wait().
      • A shorter interval means the AI will resume quickly after CanGrab.Value becomes empty, but it will consume more resources.
    • Ensuring Smooth Pausing:

      • The AI's functions (stand(), followNearestPlayer(), randomMovement()) are designed to be atomic and short.
      • By placing the pause check at the start of the loop, we ensure that the AI doesn't start a new action while it should be paused.
      • If you have long-running actions or tweens that need to be stopped immediately, additional logic would be needed to interrupt them.

    Testing the Enhanced AI:

    • Setting CanGrab.Value:

      • You can test the pausing functionality by changing the value of script.Parent.Accessory.Cap.CanGrab in the game.
      • Set the Value to any non-empty string to pause the AI.
      • Set it back to an empty string ("") to resume the AI.
    • Observing AI Behavior:

      • While paused, the AI should not perform any movements or actions.
      • Once resumed, the AI should continue from where it left off.

    Feel free to integrate this enhanced script into your game. If you have any questions or need further assistance, don't hesitate to ask!