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

Better way of adding and sticking parts to characters?

I'm new to scripting and still learning.

Okay, so when a player joins I want a Katana to be part of the player model.

I made this script for that to happen

sword = game.Lighting.Katana
weld = game.Lighting.Weld

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		
		local NewSword = sword:Clone()
		NewSword.Parent = character
		local rotation = Vector3.new(0, 0, 0)
		
		NewSword.CFrame = character:WaitForChild("Left Leg").CFrame + Vector3.new(0.55, 0.4, 0) - Vector3.new(0, 0, 1.7)
		NewSword.Rotation = rotation + Vector3.new(170, 0, -90)
		
		local ImWeld = weld:Clone()
		ImWeld.Parent = character
	end)
end)

The problem is that player walking animation doesn't play/work.

GIF: https://gyazo.com/cd306ac2e402a5738e9cb678e2e6b535

I don't know much about welding or keeping parts together so I'm using a free model weld script to hold the player and the sword together, but I'm pretty sure that is what is stopping animation from playing. I also don't know much about cframe either.

Code of free model weld script

local all,last = {}
function scan(p)
	for _,v in pairs(p:GetChildren()) do
		if v:IsA("BasePart") and v.Name ~= "Wheel" then
			if (last) then
				local w = Instance.new("Weld")
				w.Part0,w.Part1 = last,v
				w.C0 = v.CFrame:toObjectSpace(last.CFrame):inverse()
				w.Parent = last
			end
			table.insert(all,v)
			last = v
		end
		scan(v)
	end
end
scan(script.Parent)
for _,v in pairs(all) do v.Anchored = false end 

And I'm wondering if there is a better way to keep a object/part on on the player

2 answers

✓ Accepted answer

Yeah, the trick is to weld it yourself.

Take a look at the script which is welding. Take a look at it and understand what is happening. Once you understand how it works, you can understand how to only make it weld the katana to a single part of the Character instead of welding all of the Character to itself (bad)

If you need help, ask. It's better if you can do it yourself, but I'm not here to leave you in the cold on this

3 comments

0

I understand some parts of the weld scripts, but other parts I don't get it. Like I have no idea what " for _,v in pairs" means

0

Do you know any tutorials on roblox wiki, or youtube videos that can teach me about it?

0

This is the reference for Lua for loops: http://www.lua.org/pil/7.3.html - pairs is a function which returns an iterator which allows you to loop through each key=value pair in a table. Tricky, huh?

I made my own weld and welded the sword to "HumanoidRootPart" So the sword doesn't move with the leg animation.

If others need help with welding watch this video! - https://www.youtube.com/watch?v=eBMWLUAgNMs

Finished script

sword = game.Lighting.Katana
weld = game.Lighting.Weld

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local NewSword = sword:Clone()
		NewSword.Parent = character
		local LeftLeg = character:FindFirstChild("HumanoidRootPart")

		local rotation = Vector3.new(0, 0, 0)
		
		NewSword.CFrame = character:WaitForChild("Left Leg").CFrame + Vector3.new(0.55, 0.4, 0) - Vector3.new(0, 0, 1.7)
		NewSword.Rotation = rotation + Vector3.new(170, 0, -90)

		weld = Instance.new("Weld")
		weld.Part0 = LeftLeg
		weld.C0 = LeftLeg.CFrame:inverse()
		weld.Part1 = NewSword
		weld.C1 = NewSword.CFrame:inverse()
		weld.Parent = NewSword

		NewSword.CanCollide = false
		

	end)
end)

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