On Death script not working

I made a win/loss system, and I created a boolean value (called ‘Win?’). I want to make this value false on death.
I placed the script in StarterCharacterScripts and the value in StarterPlayerScripts.
However, the script does not work when I reset.
It errors: "Workspace.Xiplication.LoseWinOnDied:5: attempt to index nil with ‘Win?’"
The script:

local char = script.Parent
local hum = char:WaitForChild('Humanoid')

function removewin()
	game.Players[char.Name]:FindFirstChildWhichIsA('PlayerScripts')['Win?'].Value = false
end

hum.Died:Connect(removewin)

If anyone can, please help with this. Thank you!

1 Like

Try this?

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function removewin()
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		player.PlayerScripts:FindFirstChild("Win?").Value = false
	end
end

humanoid.Died:Connect(removewin)
1 Like

It errored. It cannot find ‘PlayerScripts’ in my character.
Error message: PlayerScripts is not a valid member of Player "Players.Xiplication"

1 Like

Try adding a WaitForChild().

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function removewin()
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		player:WaitForChild("PlayerScripts"):FindFirstChild("Win?").Value = false 
	end
end

humanoid.Died:Connect(removewin)

It warns me an infinite yield.
Infinite yield possible on 'Players.Xiplication:WaitForChild(“PlayerScripts”)'

That’s strange. Is the PlayerScripts folder named something else, perhaps?
Ive add a WhichIsA() below:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function removewin()
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		player:FindFirstChildWhichIsA("PlayerScripts"):FindFirstChild("Win?").Value = false 
	end
end

humanoid.Died:Connect(removewin)

Maybe try this:

local char = script.Parent
local hum = char:WaitForChild('Humanoid')

function removewin()
	if not game.Players.LocalPlayer:FindFirstChild("PlayerScripts") then repeat wait() until game.Players.LocalPlayer:FindFirstChild("PlayerScripts") end
	
	game.Players[char.Name]:FindFirstChildWhichIsA('PlayerScripts')['Win?'].Value = true
end

hum.Died:Connect(removewin)

It didnt work. One thing i’ve noticed is that ive looked in the explorer, and PlayerScripts is still there.

Is the code that you have given in a LocalScript or a server script?

could you send a screenshot with fully opened explorer for StarterPlayer

Its a server script.

character limit

1 Like

Okay.
I changed the code a little bit more:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local PlayerScripts = player:WaitForChild("PlayerScripts")

function removewin()
	if player then
		PlayerScripts:FindFirstChild("Win?").Value = false 
	end
end

humanoid.Died:Connect(removewin)

You should use a remotevent or remotefunction instead, PlayerScripts is created on the client, so the server can’t see this.

2 Likes

It still warns me an infinite yield for ‘PlayerScripts.’

Ohh, i see what the problem is now. Just like what @PuHoRkA has said, PlayerScripts are only viewed on the client, not the server. But why do you have the value in PlayerScripts? Are you trying to make if a person dies, only that person loses a win? Or everyone in the server?

I’m trying to make only that person lose their win value. After the game is over, i’ll reset everyone’s win values.
Also I will try to make a script which includes a remote event.

Okay. Then you can really just use a LocalScript for that matter. Remove the current script that is in StarterCharacterScripts, insert a LocalScript in StarterCharacterScripts, and then use this code:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character) -- you can also get the 'LocalPlayer'
local PlayerScripts = player:WaitForChild("PlayerScripts")

function removewin()
	if player then
		PlayerScripts:FindFirstChild("Win?").Value = false 
	end
end

humanoid.Died:Connect(removewin)
2 Likes

Finally it worked! I didn’t know I could use local scripts in characters. Thank you and @PuHoRkA for your time and assistance.

1 Like