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 9 years ago
function ontouch(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h ~= nil then
        local plr = game.Players:GetPlayerFromCharacter(h)
        if plr ~= nil then
            game.Lighting.LinkedSword:Clone().Parent = plr.Backpack
        end
    end
end

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!

2 answers

Log in to vote
2
Answered by
noliCAIKS 210 Moderation Voter
9 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:

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:

function ontouch(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)
    if plr ~= nil then
        sword = game.Lighting.LinkedSword:Clone()
        sword.Parent = plr.Backpack
    end
end
script.Parent.Touched:connect(ontouch)
0
Try adding a Debounce. woodengop 1134 — 9y
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 — 9y
0
Excellent explanation and answer. Great job! AmericanStripes 610 — 9y
Ad
Log in to vote
-2
Answered by 9 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.
function ontouch(part)
    local h = part.Parent:findFirstChild("Humanoid")
    if h ~= nil then
        local plr = game.Players:GetPlayerFromCharacter(h)
        if plr ~= nil then
            sword = game.Lighting.LinkedSword:Clone()
        sword.Parent = plr.Backpack
        end
    end
end
script.Parent.Touched:connect(ontouch)
function getPlayer(Part)
    local Humanoid = Part.Parent:FindFirstChild('Humanoid')
    if (Humanoid ~= nil) then
        local Character = Humanoid.Parent
        if (Character ~= nil) then
            return game:GetService('Players'):GetPlayerFromCharacter(Character)     
        end
    end
end

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 — 9y

Answer this question