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

Did I do the function thing wrong?

Asked by 3 years ago
script.Parent.Touched:Connect(function(plr, hit)
        local dude = hit.Parent
        local dudehuman = dude:FindFirstChild("Humanoid")
        dudehuman.Health = dudehuman.Health - 1
        plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
        wait(1)
end)

Okay so, I er wanted to make ye function, but function made use of both plr and hit, and i've seen numerous scripts where thee put a comma between ze two thingys. My problem be Me think that it only made use of plr. Me reasoning is that I had yer index nil outyputy thingzy. Me know what that means. That means that it does not exist and thy game can't be thinking doing something if thy does not exist! here is me output error.

21:11:42.691 - Workspace.Part.Script:2: attempt to index nil with 'Parent'

1
I'm sorry for pirate talk. I just felt like speaking like a pirate. ScriptsALot 91 — 3y
1
¯\_ (?)_/¯ ScriptsALot 91 — 3y

1 answer

Log in to vote
3
Answered by 3 years ago

.Touched has only one argument, which is the part that touched it. If you want to get the player, you should do :GetPlayerFromCharacter.

script.Parent.Touched:Connect(function(hit)
        local dude = hit.Parent
        local dudehuman = dude:FindFirstChild("Humanoid")
        local plr = game.Players:GetPlayerFromCharacter(dude)
        dudehuman.Health = dudehuman.Health - 1
        plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1
        wait(1)
end)
Ad

Answer this question