Create just one Instance when the player is inside a part?

What do you want to achieve? When the player walks inside of the Part, their HumanoidRootPart will get one instance of VectorForce so it will push the player upwards as long as they’re inside said Part, and it will destroy itself once the player leaves the region.

My current code adds VectorForce to HumanoidRootPart that will last for 0.2 seconds and then destroy itself…

local geyser = script.Parent

geyser.Touched:Connect(function(touch)
	local char = touch.Parent
	
	if game.Players:GetPlayerFromCharacter(char) and char:FindFirstChild("HumanoidRootPart") and not char:FindFirstChild("HumanoidRootPart"):FindFirstChild("BodyVelocity") then
		
		local vectorForce = Instance.new("VectorForce")
		
		vectorForce.Force = Vector3.new(0, 1500, 0)
		vectorForce.Parent = char.HumanoidRootPart
		vectorForce.Attachment0 = char.HumanoidRootPart.RootAttachment
		wait(0.2)
		vectorForce:Destroy()
	end
end)

…But the problem with this is that it stacks multiple VectorForces onto HumanoidRootPart as shown in the video, or the force doesn’t last enough to propel the player to the top of the Part.

Thx for reading, and if you have any suggestions feel free to reply.

2 Likes

If you add a debounce you won’t get the vector forces stacking. If there isn’t enough force initially then you should just increase the vector force amount.

1 Like

I’d use Touched and TouchedEnded to decide when the player is inside or outside of the part. Here is some pseudocode:

Touched → If character:FindFirstChild("VectorForce") == nil, then Instance.new("VectorForce")
ToucheEnded → If character:FindFirstChild("VectorForce"), then character.VectorForce:Desroy()

1 Like
local Players = game:GetService("Players")

-- Will return a Player object if passed a HumanoidRootPart
local function getPlayerFromPart(part)
    for _,player in pairs(Players:GetPlayers()) do
        local character = player.Character
        if character and part == character.HumanoidRootPart then
           return player
        end
    end
end

local geyser = script.Parent
geyser.Touched:Connect(function(part)
    -- Get Player
    local player = getPlayerFromPart(part)
    if not player then
        return
    end

    -- Search for vector force
    local humanoidRootPart = player.Character.HumanoidRootPart
    if humanoidRootPart:FindFirstChild("VectorForce") then
        return
    end

    -- Create vector force
    --todo
end)

geyser.Touched:Connect(function(part)
    -- Get Player
    local player = getPlayerFromPart(part)
    if not player then
        return
    end

    -- Destroy vector force
    local humanoidRootPart = player.Character.HumanoidRootPart
    local vectorForce = humanoidRootPart:FindFirstChild("VectorForce")
    if vectorForce then
        vectorForce:Destroy()
    end
end)
1 Like

If I helped solve you problem, would be great if you could mark my reply as the solution :smile:

Hey, thanks for the help! There’s always something new for me to learn about Lua :slightly_smiling_face:

Sorry for the late reply, but I just wanted to show the results. The base code helped a lot with the issue. :+1: