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

Attempt to Index "Player"(nil value) even though player exists?

Asked by 6 years ago
debounce = false


function onTouched(part)

if debounce == false then
    debounce = true
local h = part.Parent:findFirstChild("Humanoid")
if h ~= nil then
local Player = game.Players:GetPlayerFromCharacter(part.Parent)
Player.leaderstats.Score.Value = Player.leaderstats.Score.Value - 1
game.Players:FindFirstChild(Player.leaderstats.LastHit).leaderstats.Score.Value = game.Players:FindFirstChild(Player.leaderstats.LastHit).leaderstats.Score.Value+1
Player:LoadCharacter()
debounce = false
end
end
end
script.Parent.Touched:Connect(onTouched)


on line 11, apparently Player doesnt exist, but i tried using prints and it does.

0
Imagine if another brick touched it.......... WAIT THIS GUY HAS MESSY CODE IM NOT SUPPOSED TO GIVE ANSWERS hiimgoodpack 2009 — 6y
2
^Do not bash on peoples questions. This site is meant to help people. not bash people. PyccknnXakep 1225 — 6y
0
Sorry, hiimgoodpack, I usually make code, then go back and edit stuff in between the lines, so roblox's auto indenting doesnt kick in. It couldn't be another brick though, because in the code i use an if statement to make sure the part touching this brick is parented under a humanoid. User#17125 0 — 6y
0
Hiimgoodpack, stop acting to mental these days about the "Tabs" greatneil80 2647 — 6y
0
can you post the exact error? XAXA 1569 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

This should work for you.

local debounce = false
script.Parent.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            local stat1 = player:WaitForChild("leaderstats").Score
            stat1.Value = stat1.Value -1
            local stat2 = player:WaitForChild("leaderstats").LastHit
            stat2.Value = stat2.Value +1
        end
    end
    wait(5)
    debounce = false
end)

Please accept my answer if this helped! Thanks Edit: forgot the end, wrote this from scratch

0
Sorry, the same thing ends up happening User#17125 0 — 6y
0
This code is missing an end. CootKitty 311 — 6y
0
I know that, i fixed it, it still pops up the error... this is a real mystery XD User#17125 0 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Here is a script you can try.

debounce = false

script.Parent.Touched:connect(function(Obj)
    if Obj.Parent:FindFirstChild("Humanoid") ~= nil then
        if debounce == false then
            debounce = true
            local Player = game.Players:GetPlayerFromCharacter(Obj.Parent)
            Player.leaderstats.Score.Value = Player.leaderstats.Score.Value - 1
            Player.leaderstats.LastHit.leaderstats.Score.Value = Player.leaderstats.LastHit.leaderstats.Score.Value + 1
            Player:LoadCharacter()
            debounce = false
        end
    end
end)

If it works accept my answer. Thanks!

Answer this question