Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

why is localscript not cloning to character?

basically im trying to parent this clone of a localscript that is parented to a script in serverscriptservice to the player's character

but when i play the game the localscript wasnt there in the character model but the output printed ok which means it did work so i dont know where it went

local Seat = workspace.Model.Seat
Seat.ChildAdded:Connect(function(Child)
	if Child:IsA("Weld") and Child.Name == "SeatWeld" then
		local Player = game.Players:GetPlayerFromCharacter(Child)
		local CloneScriptChild = script.LocalScript:Clone()
		CloneScriptChild.Parent = Player print("OK!")
		
		Seat.ChildRemoved:Connect(function(Child)
			CloneScriptChild:Destroy() print("NOOOOOOOOOOOOOOOO")
		end)
	end
end)

i dont know if im just being stupid or not

4 comments

0

When the player sits in the seat the child that is a weld not the player object

0

clarifying above, the player is not parented to a seat when they sit in it, instead what's parented to the seat is a weld which attaches the player's humanoidrootpart to the seat

0

how do i fix it tho?

0

see my answer i posted

2 answers

as in my comment, welds are parented to the seat, not player objects themselves, you have a check for if it's a weld, but then immediately try to do game.Players:GetPlayerFromCharacter(Child), but since Child is a weld, Player then becomes nil since it couldnt find a player, and in the following statement CloneScriptChild.Parent = Player you just parent the script to nil, effectively removing it. you could instead,

  • look at the parents of Part0 and Part1 of the weld and get the player through GetPlayerFromCharacter that way
  • :Connect to Seat:GetPropertyChangedSignal("Occupant"), since the seat's Occupant property is set to the humanoid of the player currently sitting in it, retrieving the player object is then trivial

2 comments

0

i dont get what you mean by parents of part0 and part1 of the weld. all i know is weld is parented to seat which im assuming part0 is a seat

0

wait i understand what part0 and part1 is now but im still having a hard time piecing things together

fixed it

local Seat = workspace.Model.Seat
Seat.ChildAdded:Connect(function(Child)
	if Child:IsA("Weld") and Child.Name == "SeatWeld" then
		local Character = Child.Part1.Parent
		local Player = game.Players:GetPlayerFromCharacter(Character)--(child.Part1[HumanoidRootPart].Parent)
		local CloneScriptChild = script.LocalScript:Clone()
		CloneScriptChild.Parent = Player.Character print(Child.Part1)
		
		Seat.ChildRemoved:Connect(function(Child)
			CloneScriptChild:Destroy() print("NOOOOOOOOOOOOOOOO")
		end)
	end
end)

1 comment

0

this is the answer

This discussion is preserved as it was. New answers, comments, and votes are closed.