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

Why is this not detecting the player's backpack? [RESOLVED, THANK YOU]

Asked by 10 years ago
01function 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
09end
10 
11script.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!

2 answers

Log in to vote
2
Answered by
noliCAIKS 210 Moderation Voter
10 years ago

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:

1local 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:

1function 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
7end
8script.Parent.Touched:connect(ontouch)
0
Try adding a Debounce. woodengop 1134 — 10y
2
@ TheContinentofEurope That's probably a good idea, but it's not the issue the person was having. They would've probably figured out they should use a debounce anyway, and if not, they could just post another question for that; they are very different issues after all. noliCAIKS 210 — 10y
0
Excellent explanation and answer. Great job! AmericanStripes 610 — 10y
Ad
Log in to vote
-2
Answered by 10 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.
01function 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
10end
11script.Parent.Touched:connect(ontouch)
1function 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
9end

Try this to get it, use parts do this code I posted to get the player, may or may not work...

0
Nope, that didn't work. It doesn't matter if you make a variable and store that information, it won't work. I know the problem but I don't know how to fix it - it's not getting the player from the character for some reason. dirty_catheter 7 — 10y

Answer this question