I am currently trying to make a gas-station where you have to connect the fuel-hose to the car for it to fill up. However, there is one small problem, when I try to connect the hose (rope) to the tool, the entire player-character starts glitching around. I don’t know if it’s a bug caused by something on ROBLOX’s end or if I am doing it wrong.
The first 3 seconds of the video show the expected movement with the tool equipped.
The rest of the video shows how the hose affects the player.
Here is the function that handles the giving/ connecting:
local function giveToolFromPipe(player, pipe)
local originPart = pipe.originPart
local pipeModel = pipe.pipeModel
-- hiding the model fuel-pipe of the gas-pump
pipeModel.Transparency = 1
pipeModel.CanCollide = false
local toolClone = pipeModel.gasHose:Clone()
toolClone.Parent = player.Character
-- this keeps the player from teleporting to the tool (works on all character variants)
toolClone.Handle.Position = if (player.Character:FindFirstChild("RightArm")) then player.Character.RightArm.Position else player.Character.RightHand.Position
toolClone.Handle.Anchored = false
player.Character.Humanoid:EquipTool(toolClone)
task.wait(3)
-- this causes the character of the player to glitch around
originPart.rope.Attachment1 = toolClone.Handle.ropeConnector
toolClone.Unequipped:Connect(function()
removeToolFromPlayer(player, pipe)
end)
-- this is some cheaty fix I used for the moment.
--task.spawn(function()
-- while (inUse[pipe.Name]) do
-- pipeModel.ropeConnector.WorldCFrame = toolClone.Handle.ropeConnector.WorldCFrame
-- task.wait()
-- end
--end)
end
The CFrame of the movableConnection-Attachment is changed to it’s position where it should be using a while-isHoldingTool()-do-loop.
The fuelHose-RopeConstraint then follows it automatically.
Here is some pseudo code:
local tool = getPlayerTool(player) -- getting the tool from x-Player
local PUMP = getFuelPump() -- getting the fuel pump
-- the tool is being toggled
tool:GetAttributeChangedSignal("isHolding"):Connect(function()
-- only execute when tool is being held and the pump is available
if (tool:GetAttribute("isHolding") == false) then return end
if (PUMP:GetAttribute("available") == false) then return end
-- make the pump unavailable
PUMP:SetAttribute("available", false)
-- new thread that controls the constraint-changes
task.spawn(function()
-- loop for as long as the tool is being held
while (tool:GetAttribute("isHolding")) do
-- change the position of the attachment which is connected to the RopeConstraint
PUMP.toolRope.movableConnection.WorldCFrame = tool.connection.WorldCFrame
-- distance between the rope's origin and player
local distance = (PUM.toolRope.Attachment0.WorldCFrame.Position - player.Character.HumanoidRootPart.CFrame.Position).Magnitude
-- "offset" makes the rope hang (else it would be stiff because: length = absolute distance)
local offset = 4
-- changing the length of the rope (enabling the player to move away from the rope's origin)
PUMP.toolRope.Length = distance + offset
end
end)
-- You should always include some sort of "clear-up" method
-- in case a player dies/ leaves; the script should perform a
-- reset which resets all changed attributes/ properties to a
-- default value.
-- depending on the use-case you may also want to include
-- sanity checks. That way no exploiter can cause harm to
-- your system.
-- sanity checks could be:
-- only the holder of the tool is allowed to change the pump's "available"-status
-- only the holder of the tool can change the position of the constraint/ length of the rope
end)
I know its been 9 days, but I hope this could help you nontheless.
If you have questions, don’t be afraid to ask. I try to be more active to help you out.