01 | function ontouch(part) |
02 | local h = part.Parent:findFirstChild( "Humanoid" ) |
03 | if h ~ = nil then |
04 | local plr = game.Players:GetPlayerFromCharacter(h) |
05 | if plr ~ = nil then |
06 | game.Lighting.LinkedSword:Clone().Parent = plr.Backpack |
07 | end |
08 | end |
09 | end |
10 |
11 | script.Parent.Touched:connect(ontouch) |
I suspect that the problem is in the local plr = game.Players:GetPlayerFromCharacter(h)
line, but I'm not sure what I have done wrong. Please help me through this and be sure to explain your answer to me so that I know what the problem is in the future. Thank you!
You were right about the problem being with local plr = game.Players:GetPlayerFromCharacter(h)
. The GetPlayerFromCharacter method should be used on character models, not humanoids; so instead, you should do this:
1 | local plr = game.Players:GetPlayerFromCharacter(part.Parent) |
You can also use h.Parent
instead of part.Parent
if you want, but it's a bit redundant to look for the Humanoid if you're just going to use the parent anyway. Instead, you can leave the whole check if there is a humanoid out, and just do:
1 | function ontouch(part) |
2 | local plr = game.Players:GetPlayerFromCharacter(part.Parent) |
3 | if plr ~ = nil then |
4 | sword = game.Lighting.LinkedSword:Clone() |
5 | sword.Parent = plr.Backpack |
6 | end |
7 | end |
8 | script.Parent.Touched:connect(ontouch) |
01 | function ontouch(part) |
02 | local h = part.Parent:findFirstChild( "Humanoid" ) |
03 | if h ~ = nil then |
04 | local plr = game.Players:GetPlayerFromCharacter(h) |
05 | if plr ~ = nil then |
06 | sword = game.Lighting.LinkedSword:Clone() |
07 | sword.Parent = plr.Backpack |
08 | end |
09 | end |
10 | end |
11 | script.Parent.Touched:connect(ontouch) |
1 | function getPlayer(Part) |
2 | local Humanoid = Part.Parent:FindFirstChild( 'Humanoid' ) |
3 | if (Humanoid ~ = nil ) then |
4 | local Character = Humanoid.Parent |
5 | if (Character ~ = nil ) then |
6 | return game:GetService( 'Players' ):GetPlayerFromCharacter(Character) |
7 | end |
8 | end |
9 | end |
Try this to get it, use parts do this code I posted to get the player, may or may not work...