WeldConstraint sometimes doesnt work,tryng to make npc carry player

I have no idea what causes this problem maybe the problem is not the WeldConstraint may be related to networkowner, IDK when the player network owner is set to server, client pov just freezes (I sent a video of it), I thought it was a bug with the Roblox studio because when I test my game on Roblox studio the npc and player always freezes (no movement), when I test it on a live server outside Roblox studio most of the time works fine(6/10)


function AILibrary.StickPlayer(p)
 
		local character = p.Character or p.CharacterAdded:Wait()
		for _ ,part in ipairs(character:GetDescendants()) do
			if part:IsA("BasePart") then
				part.Massless = true
				pyshic:SetPartCollisionGroup(part,"grabbed") --make player cant collide to all object in workspace
			end
		end
		local humanoid = character:FindFirstChild("Humanoid")
		local hrp = character:FindFirstChild("HumanoidRootPart")
	
		local seat = basenpc:FindFirstChild("seat") --npc
	
		character.Archivable = true
		hrp.CFrame = seat.CFrame
	
		local weld = Instance.new("WeldConstraint")
		weld.Parent = hrp
		weld.Part0 = seat
		weld.Part1 = hrp
		
		humanoid.PlatformStand = true
		character.Data.grabbed.Value = true
		task.wait(3)
end
function AILibrary.UnStickPlayer(p)
	local character = p.Character or p.CharacterAdded:Wait()
	for _ ,part in ipairs(character:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Massless = false
			pyshic:SetPartCollisionGroup(part,"Player")
		end
	end
	local humanoid = character:FindFirstChild("Humanoid")
	local hrp = character:FindFirstChild("HumanoidRootPart")
	local weld = hrp:FindFirstChild("WeldConstraint")
	if weld then
		weld:Destroy()
	end
	humanoid.PlatformStand = false
	character.Archivable = false
	character.Data.grabbed.Value = false
end

preview when it works

preview when it doesn’t work

idk why it sometimes works and sometimes fails

I don’t think the functions are the problem, the only thing I can say is to check if the “UnStickPlayer” function was called incorrectly. without the rest of the scripts, it’s hard to help you

try to set players humanoid platformstand property to true

here is rest of the code

if (target.Position - myRoot.Position).Magnitude < 3 then
	 local character = tplayer.Character or tplayer.CharacterAdded:Wait()
	 lib.playJumpscare(tplayer)
	 lib.StickPlayer(tplayer)
	 movetoKnockPos(tplayer)
	 break
 end


function movetoKnockPos(p)
	lib.SetState("grabplayer")
	local arraypos = knocarraypos:GetChildren()
	local randomid = math.random(1, #arraypos)
	local wpknock = arraypos[randomid].Position
	local start = myRoot.Position
	local path = pathFinding:FindPath(start, wpknock)
	lib.ViewWayPoints(path)
	if path then
		table.insert(path,1,start)
		table.insert(path,wpknock)
		for i = 1 ,#path do
			local pos = path[i]

			myHuman:MoveTo(pos)
			local timeOut = myHuman.MoveToFinished:Wait()
			if not timeOut then
				print("TP To Spawn")
				myRoot.CFrame = resetpos.CFrame + Vector3.new(0,10,0)
				task.wait(2)
				break
			end
		end
		task.wait(3)
		lib.UnStickPlayer(p)


	else

		task.wait(3)
		lib.UnStickPlayer(p)


	end
end

well you can also use tween service if im not wrong

I did that on the post I put the code, also tested use seat, sit = true, has same problem

well in the second preweiw i dont see the skeleton in the cutscene or what is it

if (target.Position - myRoot.Position).Magnitude < 3 then
	 local character = tplayer.Character or tplayer.CharacterAdded:Wait()
	 lib.playJumpscare(tplayer)
	 lib.StickPlayer(tplayer)
	 movetoKnockPos(tplayer)
	 break
 end


function movetoKnockPos(p)
	lib.SetState("grabplayer")
	local arraypos = knocarraypos:GetChildren()
	local randomid = math.random(1, #arraypos)
	local wpknock = arraypos[randomid].Position
	local start = myRoot.Position
	local path = pathFinding:FindPath(start, wpknock)
	lib.ViewWayPoints(path)
	if path then
		table.insert(path,1,start)
		table.insert(path,wpknock)
		for i = 1 ,#path do
			local pos = path[i]

			myHuman:MoveTo(pos)
			local timeOut = myHuman.MoveToFinished:Wait()
			if not timeOut then
				print("TP To Spawn")
				myRoot.CFrame = resetpos.CFrame + Vector3.new(0,10,0)
				task.wait(2)
				break
			end
		end
		task.wait(3)
		lib.UnStickPlayer(p)


	else

		task.wait(3) -- delete this
		lib.UnStickPlayer(p) -- delete this


	end
end

try deleting these

yea for no reason the skeleton is stick on behind me this is so weird,
work fine on server POV, broken on Client POV

here is the video with “are owner shown” setting turned on on studio setting

I just tested it and still has the same problem, looks fine on the server pov, but freezes on the client pov, I sent you the video in my previous comment

Here is some troubleshooting steps you can take, that could be causing the issue. Answer is taken from ChatGPT, a programming AI Robot:
"
There could be a number of reasons why your code sometimes works and sometimes fails. Here are a few potential issues you might want to consider:

  1. Race conditions: It’s possible that the player’s character is not fully loaded when you try to access it, or that other parts of the character are still being created. This could cause errors or unexpected behavior. To avoid this, you can use the p.CharacterAdded event to wait until the character is fully loaded before executing your code.
  2. Concurrency issues: If multiple parts of your code are trying to access and modify the same objects at the same time, you could end up with unexpected behavior. To avoid this, you might want to consider using Roblox’s Lock function to ensure that only one part of your code is executing at a time.
  3. Networking issues: If you are running your code on a live server, there could be issues with the way that data is being synchronized between the client and the server. This could cause the client’s view to freeze or become out of sync with the server.

To troubleshoot these issues, you might want to try adding some print statements to your code to see what is happening at each step, and to look for any error messages that might be occurring. You can also try running your code on a local server to see if the issues persist.
"

thank you for the very detailed comment I really appreciate it,
for point number 1, I do a test add wait for 20s to make sure the character is fully loaded, even I don’t think this is the problem because the character is never unloaded, but I did test it to make sure

and for point number 2 , I’m pretty sure there are no multiple codes trying to modify the same object at one time, I’m sorry but are you talking about lock on the home button? isn’t that make we cannot move or click the part? cmiiw

and point number 3 is so interesting, I did test it on a local server and I found an abnormal behavior, actually, this also happened on live server,
so here is my code to make the player tp to the skeleton part

local seat = basenpc:FindFirstChild("seat ") --npc part to grab or hold player position
local hrp = character:FindFirstChild("HumanoidRootPart") --player hrp

local weld = Instance.new("WeldConstraint")
        weld.Parent = hrp
	weld.Part0 = seat 
	weld.Part1 = hrp

hrp.CFrame = seat.CFrame


as you can see I put

hrp.CFrame = seat.CFrame

that should make the player teleport to the skeleton part, right?

on server pov it does a normal thing, make player teleport to the skeleton part

but from the client pov its make the skeleton teleport to player

here is the video

I really don’t understand how this happened, and I’m pretty sure there is no error, I did debug each step

I found the problem, the problem is first-person camera mode,

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