Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

5 comments

0

Imagine if another brick touched it.......... WAIT THIS GUY HAS MESSY CODE IM NOT SUPPOSED TO GIVE ANSWERS

2

^Do not bash on peoples questions. This site is meant to help people. not bash people.

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.

0

Hiimgoodpack, stop acting to mental these days about the "Tabs"

0

can you post the exact error?

2 answers

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

3 comments

0

Sorry, the same thing ends up happening

0

This code is missing an end.

0

I know that, i fixed it, it still pops up the error... this is a real mystery XD

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.