Defining plr in a serverscript

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)

ThirstVal does exist, it is inside of the player,and is simply a value.
image
The V is not lowercase.

I see, while printing “plr”, is it actually the player or something else?

Printing PLR just prints my username, so it is printing the user.

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.

Did you add those player values locally or from a server script?

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.

serverscript, which most likely is causing the issue, however the plr definition is made for a serverscript

It printed this:
image
it seems like the script is finding the player but for some reason can’t find the value, it is in player though.

You’re most likely creating the ThirstVal instance on the client. Make sure it’s created on the server if that is the case.

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.

So you are using a local script to create ThirstVal but above you said you are using a serverscript? I am confused here.

1 Like

Sorry its confusing.

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.

You can detect when a player joins on the server using Players.PlayerAdded:

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)
1 Like

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.

1 Like

This worked perfectly, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.