Spawn bubble causing player to teleport back to walked in location

I am trying to have it so that when the player is inside the safe zone aka this big box they have this spawnbubble around them thats locked to them only while in the zone

The issue is whenever the player walks in the safe zone they are briefly teleported back to the spot they walked in on and while in the safe zone the players walk animation is kinda stiff.

I tried using motor6d or cframe position updating but they both do the same thing well cframe position updatng doesnt but the bubble follows the player very laggily.


local safeZone = workspace:WaitForChild("SafeZone") -- Your box-shaped SafeZone part
local ReplicatedStorage = game.ReplicatedStorage
local bubbleEvent = ReplicatedStorage:WaitForChild("BubbleEvent") -- RemoteEvent for bubble control
local bubbleTemplate = ReplicatedStorage:WaitForChild("SpawnBubble") -- The original bubble mesh in ReplicatedStorage

-- Function to check if a player is inside the safe zone
local function isPlayerInsideZone(player)
	local character = player.Character
	if not character then return false end

	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return false end

	-- Check if the player's HumanoidRootPart is inside the SafeZone part
	local zonePosition = safeZone.Position
	local zoneSize = safeZone.Size

	return humanoidRootPart.Position.X > (zonePosition.X - zoneSize.X / 2) and humanoidRootPart.Position.X < (zonePosition.X + zoneSize.X / 2) and
		humanoidRootPart.Position.Y > (zonePosition.Y - zoneSize.Y / 2) and humanoidRootPart.Position.Y < (zonePosition.Y + zoneSize.Y / 2) and
		humanoidRootPart.Position.Z > (zonePosition.Z - zoneSize.Z / 2) and humanoidRootPart.Position.Z < (zonePosition.Z + zoneSize.Z / 2)
end

-- Table to track active bubbles
local activeBubbles = {}

-- Function to create a bubble for the player inside the safe zone
local function createBubbleForPlayer(player)
	local character = player.Character
	if not character then return end

	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return end

	-- Create the bubble and place it near the player's HumanoidRootPart
	local bubble = bubbleTemplate:Clone()
	bubble.Parent = workspace
	bubble.Name = player.Name .. "_Bubble"
	bubble.Size = Vector3.new(8.5, 8.5, 8.5)

	-- Set the bubble's initial position to the player's HumanoidRootPart position
	bubble.CFrame = humanoidRootPart.CFrame

	-- Create a WeldConstraint to attach the bubble to the player's HumanoidRootPart
	local weld = Instance.new("WeldConstraint")
	weld.Parent = bubble
	weld.Part0 = humanoidRootPart
	weld.Part1 = bubble

	-- Add to activeBubbles table
	activeBubbles[player] = bubble
end

-- Handle damage prevention, ForceField, and bubble display
game:GetService("RunService").Heartbeat:Connect(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local character = player.Character
		if character then
			local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
			local forceField = character:FindFirstChildOfClass("ForceField") -- Look for existing ForceField

			if humanoidRootPart then
				-- Check if the player is inside the safe zone
				if isPlayerInsideZone(player) then
					-- Create ForceField if not present
					if not forceField then
						forceField = Instance.new("ForceField")
						forceField.Parent = character
						forceField.Visible = false -- Make the ForceField invisible
					end

					-- Check if the bubble is already created for this player
					local bubble = activeBubbles[player]
					if not bubble then
						-- Create the bubble
						createBubbleForPlayer(player)

						-- Fire event to clients to show the bubble
						bubbleEvent:FireAllClients(player, true)
					end
				else
					-- Remove ForceField and bubble when player exits the safe zone
					if forceField then
						forceField:Destroy()
					end

					-- Destroy the bubble
					local bubble = activeBubbles[player]
					if bubble then
						bubble:Destroy()
						activeBubbles[player] = nil

						-- Fire event to clients to hide the bubble
						bubbleEvent:FireAllClients(player, false)
					end
				end
			end
		end
	end
end)

Any help is appreciated

2 Likes

I didn’t understand your question well but I think this is what you mean: (didnt test this)

local safeZone = workspace:WaitForChild("SafeZone") -- Your box-shaped SafeZone part
local ReplicatedStorage = game.ReplicatedStorage
local bubbleEvent = ReplicatedStorage:WaitForChild("BubbleEvent") -- RemoteEvent for bubble control
local bubbleTemplate = ReplicatedStorage:WaitForChild("SpawnBubble") -- The original bubble mesh in ReplicatedStorage

-- Function to check if a player is inside the safe zone
local function isPlayerInsideZone(player)
    local character = player.Character
    if not character then return false end

    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    if not humanoidRootPart then return false end

    -- Check if the player's HumanoidRootPart is inside the SafeZone part
    local zonePosition = safeZone.Position
    local zoneSize = safeZone.Size

    return humanoidRootPart.Position.X > (zonePosition.X - zoneSize.X / 2) and humanoidRootPart.Position.X < (zonePosition.X + zoneSize.X / 2) and
        humanoidRootPart.Position.Y > (zonePosition.Y - zoneSize.Y / 2) and humanoidRootPart.Position.Y < (zonePosition.Y + zoneSize.Y / 2) and
        humanoidRootPart.Position.Z > (zonePosition.Z - zoneSize.Z / 2) and humanoidRootPart.Position.Z < (zonePosition.Z + zoneSize.Z / 2)
end

-- Table to track active bubbles
local activeBubbles = {}

-- Function to create a bubble for the player inside the safe zone
local function createBubbleForPlayer(player)
    local character = player.Character
    if not character then return end

    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
    if not humanoidRootPart then return end

    -- Create the bubble and place it near the player's HumanoidRootPart
    local bubble = bubbleTemplate:Clone()
    bubble.Parent = workspace
    bubble.Name = player.Name .. "_Bubble"
    bubble.Size = Vector3.new(8.5, 8.5, 8.5)

    -- Set the bubble's initial position to the player's HumanoidRootPart position
    bubble.CFrame = humanoidRootPart.CFrame

    -- Create a BodyPosition to smoothly follow the player without interfering with movement
    local bodyPosition = Instance.new("BodyPosition")
    bodyPosition.MaxForce = Vector3.new(10000, 10000, 10000)
    bodyPosition.D = 1000  -- Damping for smooth movement
    bodyPosition.P = 5000   -- Power for responsiveness
    bodyPosition.Parent = bubble

    -- Update the bubble's position every frame to follow the player smoothly
    game:GetService("RunService").Heartbeat:Connect(function()
        if character and humanoidRootPart then
            bodyPosition.Position = humanoidRootPart.Position + Vector3.new(0, 5, 0)  -- Adjust position as needed
        end
    end)

    -- Add to activeBubbles table
    activeBubbles[player] = bubble
end

-- Handle damage prevention, ForceField, and bubble display
game:GetService("RunService").Heartbeat:Connect(function()
    for _, player in pairs(game.Players:GetPlayers()) do
        local character = player.Character
        if character then
            local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
            local forceField = character:FindFirstChildOfClass("ForceField") -- Look for existing ForceField

            if humanoidRootPart then
                -- Check if the player is inside the safe zone
                if isPlayerInsideZone(player) then
                    -- Create ForceField if not present
                    if not forceField then
                        forceField = Instance.new("ForceField")
                        forceField.Parent = character
                        forceField.Visible = false -- Make the ForceField invisible
                    end

                    -- Check if the bubble is already created for this player
                    local bubble = activeBubbles[player]
                    if not bubble then
                        -- Create the bubble
                        createBubbleForPlayer(player)

                        -- Fire event to clients to show the bubble
                        bubbleEvent:FireAllClients(player, true)
                    end
                else
                    -- Remove ForceField and bubble when player exits the safe zone
                    if forceField then
                        forceField:Destroy()
                    end

                    -- Destroy the bubble
                    local bubble = activeBubbles[player]
                    if bubble then
                        bubble:Destroy()
                        activeBubbles[player] = nil

                        -- Fire event to clients to hide the bubble
                        bubbleEvent:FireAllClients(player, false)
                    end
                end
            end
        end
    end
end)

Hopefully this is what you meant

I had already fixed it but thank you

1 Like

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