I am trying to make a survived system by inserting a value inside of every player called Survived, and it is set to true when you are inside the round area. This is done by touching a part with a script inside of that part and this worked perfectly fine. However, I have created a script inside ServerScriptService that changes the value to false when the player had died but it is not working and when I check the survived value inside of my player has not changed. Is there an efficient way to fix this issue?
The system works by using a value inside the workspce which displays the players username for other scripts to access.
This is the code inside the script in ServerScriptService:
local plrValue = game.Workspace.PlrValue -- value inside workspace which is for the players username
local text = plrValue.Value
game.Players.PlayerAdded:Connect(function(plr)
local survived = Instance.new("BoolValue")
survived.Parent = game.Players:FindFirstChild(plr.Name)
survived.Name = "survived"
survived.Value = false
plr.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
survived.Value = false
end)
end)
end)
And below is the script inside of the Part that the player touches to change the value to true. This script works
local plrValue = game.Workspace.PlrValue
function onTouched(hit)
if hit.Parent:findFirstChild("Humanoid") then
local survived = game.Players:findFirstChild(hit.Parent.Name).survived
survived.Value = true
plrValue.Value = hit.Parent.Name
end
end
script.Parent.Touched:connect(onTouched)
I get an error message from the script inside of the part that is touched saying “Attempt to index nil with survived ”, meaning that something is wrong with the hirearchy in that it is not accessing the player’s value.
The error message means that you can’t get survived fom the player because the player variable is nil
Try doing this instead
local plrValue = game.Workspace.PlrValue
local Players = game:GetService("Players")
function onTouched(hit)
if hit.Parent:findFirstChild("Humanoid") then
local Character = hit.Parent
-- This is the correct way of getting the player from the character
local Player = Players:GetPlayerFromCharacter(Character)
local survived = Player:WaitForChild("survived")
survived.Value = true
plrValue.Value = hit.Parent.Name
end
end
script.Parent.Touched:connect(onTouched)
No errors are printed. I am trying to attach a video of a screen recording but it says “file could not pe uploaded”, although it’s not relaly relevant though.
The character is spawned before the CharacterAdded runs, it will only work after the player dies for the first time, do this instead
local function CharAdded(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Once(function() --prevent memory leaks
survived.Value = false
end)
end
game.Players.PlayerAdded:Connect(function(plr)
script.Enabled = false
local survived = Instance.new("BoolValue")
survived.Parent = game.Players:FindFirstChild(plr.Name)
survived.Name = "survived"
survived.Value = false
if player.Character then CharAdded(player.Character) end
plr.CharacterAdded:Connect(CharAdded)
end)
Put the function outside PlayerAdded and I misspelt player, sorry
local plrValue = game.Workspace.PlrValue -- value inside workspace which is for the players username
local text = plrValue.Value
local function CharAdded(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Once(function() --prevent memory leaks
survived.Value = false
end)
end
game.Players.PlayerAdded:Connect(function(plr)
script.Enabled = false
local survived = Instance.new("BoolValue")
survived.Name = "survived"
survived.Value = false
survived.Parent = plr
if plr.Character then CharAdded(plr.Character) end
plr.CharacterAdded:Connect(CharAdded)
end)
The function does not work as the Survived variable is defigned within the player added functions so it cannot be called before then, it needs to be defined outside the PlayerAdded function. That’s why I tried putting it inside origionally.
Oh right, you can just send the value to the function
local plrValue = game.Workspace.PlrValue -- value inside workspace which is for the players username
local text = plrValue.Value
local function CharAdded(character, survived)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Once(function() --prevent memory leaks
survived.Value = false
end)
end
game.Players.PlayerAdded:Connect(function(plr)
script.Enabled = false
local survived = Instance.new("BoolValue")
survived.Name = "survived"
survived.Value = false
survived.Parent = plr
if plr.Character then CharAdded(plr.Character, survived) end
plr.CharacterAdded:Connect(function(character)
CharAdded(character, survived)
end)
end)
Or if you prefer putting it inside then put it right after parenting survived (with the script before, not this script)
Thanks for the script, however it still does not work I’m afraid. There is no error message so I am very confused.
Edit: There is an error message when touching the part that sets the value to true saying “Attempt to index nil with WaitForChild”
Here is the script inside of the part:
local plrValue = game.Workspace.PlrValue
local Players = game:GetService("Players")
function onTouched(hit)
if hit.Parent:findFirstChild("Humanoid") then
local Character = hit.Parent
-- This is the correct way of getting the player from the character
local Player = Players:GetPlayerFromCharacter(Character)
local survived = Player:WaitForChild("survived")
survived.Value = true
plrValue.Value = hit.Parent.Name
end
end
script.Parent.Touched:connect(onTouched)
Oh wait, when the player dies in the zone the touch script that enables survived keeps running and makes the value true. You can do this to ensure it doesn’t do that when the player dies (the touch script):
local plrValue = game.Workspace.PlrValue
local Players = game:GetService("Players")
function onTouched(hit)
local Character = hit.Parent
local Player = Players:GetPlayerFromCharacter(Character)
if not Player then return end
local humanoid = Character:FindFirstChild("Humanoid")
if humanoid.Health <= 0 then return end
local survived = Player:WaitForChild("survived")
survived.Value = true
plrValue.Value = hit.Parent.Name
end
script.Parent.Touched:connect(onTouched)