I am using a serverscript to fire a remoteevent, and I need to define two things:
Thristval: a value that is in the player (not in workspace)
Plr: what I am using to find the player.
The code I have now returns the error “Infinite yield possible on 'Players.XjurrasicX:WaitForChild(“ThirstVal”)”
I’m unsure if this will work in a serverscript, but otherwise besides this error it works fine.
My current code:
game.workspace.WaterDrinkFolder.WaterDetection.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
print(plr)
local ThirstVal = plr:WaitForChild("ThirstVal")
if hit.parent.ThirstVal < 100 then
game.replicatedstorage.RemoteEvent:Fire()
print("Event Successfully Fired!")
elseif hit.parent.ThirstVal > 100 then
print("Too high ThirstVal")
ThirstVal = 100
end
end)
It seems that “ThirstVal” doesn’t exist. I see that you said “Thirstval” in the topic, i.e the “V” is lowercase. Try this then;
game.workspace.WaterDrinkFolder.WaterDetection.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
print(plr)
if plr then -- Also use an "if-statement" to see if "plr" actually exists.
local ThirstVal = plr:WaitForChild("Thirstval")
if hit.parent.ThirstVal.Value < 100 then -- You should also add a .Value to the value or else it will cause errors!
game.replicatedstorage.RemoteEvent:Fire()
print("Event Successfully Fired!")
elseif hit.parent.ThirstVal.Value > 100 then
print("Too high ThirstVal")
ThirstVal = 100
end
end
end)
Just tested it, I added a value into the workspace character and it still says the same error, so the plr isn’t finding the workspace player, im clueless why it doesn’t work.
Hmm, this is quite odd… Can you try using; print(plr:GetChildren())
It would basically print all the instances in the player and if “ThirstVal” exists it would let us know that.
Alright, how exactly would I recreate it on the server then?
Here’s how it’s created currently:
local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxThirst = 100
local DecreaseRate = 5 -- 8 seconds = -1 hunger
local ThirstValue
if Plr:FindFirstChild("ThirstVal") then
ThirstValue = Plr.HungerVal
ThirstValue.Value = MaxThirst
Could I simply define the ThirstValue in the same way but in a serverscript?
I’m using a localscript right now to create it, which I probably should have
noticed as the issue earlier.
I am using a serverscript to detect when a part is touched to fire an event.
The ThirstValue is a seperate system, which is a simple thirst system. It is defined and
created inside of a local script.
local Players = game:GetService(“Players”)
Players.PlayerAdded:Connect(function(Player)
-- Create the instance and parent it under player
local ThirstValue = Instance.new(“NumberValue”) --
ThirstValue.Name = “ThirstVal”
ThirstValue.Parent = Player
end)
I see. Well you can do many things to make the “ThirstVal”. Something like ProgrammingRottie has shown can work. Or alternatively a stupid way to still fix the issue without the usage of converting it into a serverscript could be to convert your touch detection script into a local script. Though, it’s better to do what ProgrammingRottie has suggested as it allows you more flexibility rather than my idea which has some limitations.