While scripting for my game I came across this issue where:
When welding a player to an object, then trying to position the player away from that object and delete the weld causes it to either go two ways depending on what line of code came first
1: Move the player THEN the delete the weld
player is moved (alongside the welded part which I don’t want), but the weld is not deleted
2: delete the weld THEN move the player
weld is deleted, but the player is not moved
From my testing it seems that waiting 0.1 seconds (using task.wait) between each line resolves this, but I require this to happen near instantly, as to not break player immersion.
here’s the code with the bare necessities to recreate the problem
as well as a place file: weld_Issues.rbxl (60.2 KB)
--this script is nearly an exact copy of what is happening in my game
--but I am excluding a couple things that are not neccesary to replicate what is happening
local outside = workspace.Outside
--structured like this
--[[
| -- Model
\
| -- Welds
| -- SendTo
| -- Primary
\
| -- ClickDetector
]]
local inside = workspace.Inside
--structured like this
--[[
| -- Model
\
| -- Welds
| -- SendTo
| -- Primary
\
| -- ClickDetector
]]
local playersOutside = {}
--The destination is already pre-determined
--This function seems to work
local function sendPlayerOutSide(player: Player)
local weld: Weld = Instance.new("Weld")
weld.Name = player.Name
weld.Part0 = outside.SendTo
weld.Part1 = player.Character.PrimaryPart
-- `outSide.Welds` folder to contain all the welds
weld.Parent = outside.Welds
player.Character:PivotTo(outside.SendTo.CFrame)
table.insert(playersOutside, player.Character)
end
--use this to switch how the lines are read as mentioned in my post
local switch = true
local function sendPlayerInside(player: Player)
for i,v in ipairs(playersOutside) do
if v == player.Character then
if switch then
outside.Welds:FindFirstChild(player.Name):Destroy()
player.Character:PivotTo(inside.SendTo.CFrame)
playersOutside[i] = nil
else
player.Character:PivotTo(inside.SendTo.CFrame)
outside.Welds:FindFirstChild(player.Name):Destroy()
playersOutside[i] = nil
end
end
end
end
-- the inside area and outside area are supposed to be seperated so the player can reach them
--but for ease of access i've put them next to each other with indicators
outside.Primary.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player)
sendPlayerInside(playerWhoClicked)
end)
inside.Primary.ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player)
sendPlayerOutSide(playerWhoClicked)
end)
*Edit – Minor grammatical error + added structure to where models are assigned + moved some stuff around