Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Better way of adding and sticking parts to characters?

Asked by 9 years ago

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

01sword = game.Lighting.Katana
02weld = game.Lighting.Weld
03 
04game.Players.PlayerAdded:connect(function(player)
05    player.CharacterAdded:connect(function(character)
06 
07        local NewSword = sword:Clone()
08        NewSword.Parent = character
09        local rotation = Vector3.new(0, 0, 0)
10 
11        NewSword.CFrame = character:WaitForChild("Left Leg").CFrame + Vector3.new(0.55, 0.4, 0) - Vector3.new(0, 0, 1.7)
12        NewSword.Rotation = rotation + Vector3.new(170, 0, -90)
13 
14        local ImWeld = weld:Clone()
15        ImWeld.Parent = character
16    end)
17end)

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

01local all,last = {}
02function 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.Part0,w.Part1 = last,v
08                w.C0 = 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
16end
17scan(script.Parent)
18for _,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

Log in to vote
0
Answered by 9 years ago

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

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 NovaMagic 73 — 9y
0
Do you know any tutorials on roblox wiki, or youtube videos that can teach me about it? NovaMagic 73 — 9y
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? User#6546 35 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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

01sword = game.Lighting.Katana
02weld = game.Lighting.Weld
03 
04game.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 = Vector3.new(0, 0, 0)
11 
12        NewSword.CFrame = character:WaitForChild("Left Leg").CFrame + Vector3.new(0.55, 0.4, 0) - Vector3.new(0, 0, 1.7)
13        NewSword.Rotation = rotation + Vector3.new(170, 0, -90)
14 
15        weld = Instance.new("Weld")
View all 26 lines...

Answer this question