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
01 | sword = game.Lighting.Katana |
02 | weld = game.Lighting.Weld |
03 |
04 | game.Players.PlayerAdded:connect( function (player) |
05 | player.CharacterAdded:connect( function (character) |
06 |
07 | local NewSword = sword:Clone() |
08 | NewSword.Parent = character |
09 | local rotation = Vector 3. new( 0 , 0 , 0 ) |
10 |
11 | NewSword.CFrame = character:WaitForChild( "Left Leg" ).CFrame + Vector 3. new( 0.55 , 0.4 , 0 ) - Vector 3. new( 0 , 0 , 1.7 ) |
12 | NewSword.Rotation = rotation + Vector 3. new( 170 , 0 , - 90 ) |
13 |
14 | local ImWeld = weld:Clone() |
15 | ImWeld.Parent = character |
16 | end ) |
17 | 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
01 | local all,last = { } |
02 | function scan(p) |
03 | for _,v in pairs (p:GetChildren()) do |
04 | if v:IsA( "BasePart" ) and v.Name ~ = "Wheel" then |
05 | if (last) then |
06 | local w = Instance.new( "Weld" ) |
07 | w.Part 0 ,w.Part 1 = last,v |
08 | w.C 0 = v.CFrame:toObjectSpace(last.CFrame):inverse() |
09 | w.Parent = last |
10 | end |
11 | table.insert(all,v) |
12 | last = v |
13 | end |
14 | scan(v) |
15 | end |
16 | end |
17 | scan(script.Parent) |
18 | 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
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
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
01 | sword = game.Lighting.Katana |
02 | weld = game.Lighting.Weld |
03 |
04 | game.Players.PlayerAdded:connect( function (player) |
05 | player.CharacterAdded:connect( function (character) |
06 | local NewSword = sword:Clone() |
07 | NewSword.Parent = character |
08 | local LeftLeg = character:FindFirstChild( "HumanoidRootPart" ) |
09 |
10 | local rotation = Vector 3. new( 0 , 0 , 0 ) |
11 |
12 | NewSword.CFrame = character:WaitForChild( "Left Leg" ).CFrame + Vector 3. new( 0.55 , 0.4 , 0 ) - Vector 3. new( 0 , 0 , 1.7 ) |
13 | NewSword.Rotation = rotation + Vector 3. new( 170 , 0 , - 90 ) |
14 |
15 | weld = Instance.new( "Weld" ) |