My game has a sanity meter, and when it reaches 0, the player dies and the bar is refilled on respawn. I’m trying to give the player a tool (Almond water) when they die from their sanity reaching 0.
This is my current script:
local value = game.Players.LocalPlayer.PlayerGui:WaitForChild("SanityGui").Holder.Bar.Value
local tool = game.ReplicatedStorage["Almond Water"]
local charName = value.Parent.Parent.Parent.Parent.Parent.Name
local db = true
while task.wait() do
print("test1")
if value.Value <= 0 then
print("test2")
if db == true then
print("test3")
task.wait(3)
tool:Clone().Parent = game.Players[charName].Backpack
db = false
print("test4")
end
end
end
Currently I get the error " 12:17:41.344 Workspace.Script:1: attempt to index nil with ‘PlayerGui’ - Server - Script:1"
I’ve searched for other topics, but the other topics are confusing. Originally this script was under the value itself, but it didn’t work there so I moved it to ServerScriptService.
You can’t access a LocalPlayer from the server. Either track the value directly on the server (pretty sure you can’t access PlayerGui from the server, so it has to be it’s own variable/NumberValue), or call an event from the client when the client sees the value is at 0 (make sure to do checks with this however, as the client can fire this at will).
I’m not sure how to do that. The value is a NumberValue. All I need to do is find the value, detect when it’s at 0, and 3 seconds later give the Almond Water to the player that died from the value reaching 0.
If you wished to keep it simple, just have an event that the client fires to the server when it sees the value is at 0. Then, the server will listen to that event, and then run the code to respawn the player with the item.
However, it would probably be better to store the value on the server in case the player tries to spoof it.
You can fire an event from the server to client in intervals (~1 second), storing the value (assuming the value naturally drops). And for extra responsiveness, fire the event regardless of the interval when there is a major change (from a jumpscare or something) so there isn’t any noticeable delay in the value dropping.
Then, you can easily access that value from the server to read from/write to.
You could do this with remote events like what @TestyLike3 said in post #1, I’ll show you the framework. Put a local script in StarterPlayerScripts first.
local value = game.Players.LocalPlayer.PlayerGui:WaitForChild("SanityGui").Holder.Bar.Value
local tool = game.ReplicatedStorage["Almond Water"]
local charName = value.Parent.Parent.Parent.Parent.Parent.Name
local db = true
while task.wait() do
print("test1")
if value.Value <= 0 then
print("test2")
if db == true then
print("test3")
task. Wait(3)
game.ReplicatedStorage.GiveWater:FireServer(tool: Clone())
db = false
print("test4")
end
end
end