✓ Accepted answer
Improvements
Use :GetService() to retrieve the ServerStorage service
Use :WaitForChild() to make sure an instance exists before using it
Move the while loop outside the function (if there would be multiple in your code, you can use the spawn() function to run it in a new thread)
Issues
If the condition in your while loop is not met, you will have an infinite loop and crash the server, add a wait outside the condition.
Use an if statement to check the FindFirstChild since if it returns nil then you will receive an error dictating that "stats" doesn't have a Value property
Explanation
You can't get information from the ServerStorage on the Client and you would need a RemoteEvent if you wanted to switch to the Client in order to keep data updated on the Server.
Your "f" variable is affected by all players since it is connected to the server [Line 24 in the original script].
You can check for a player sitting using the HumanoidStateType Seated
In the below, I'm assuming your "Owner" is an ObjectValue
Revised Server Script
local Players = game:GetService("Players")
local money = game:GetService("ServerStorage"):WaitForChild("PlayerMoney")
local parent = script.Parent
local screenn = parent.Parent.Parent:WaitForChild("monitor"):WaitForChild("screen")
local owner = parent.Parent.Parent.Parent.Parent:WaitForChild("Owner")
parent.ChildAdded:Connect(function(Child)
local player = owner.Value
if Child.Name == "SeatWeld" and player and player:IsDescendantOf(Players) == true then
screenn.BrickColor = BrickColor.new("Steel blue")
end
end)
parent.ChildRemoved:Connect(function(Child)
local player = owner.Value
if Child.Name == "SeatWeld" and player and player:IsDescendantOf(Players) == true then
screenn.BrickColor = BrickColor.new("Black")
end
end)
while true do
local player = owner.Value
if player and player:IsDescendantOf(Players) == true then
local stats = money:FindFirstChild(player.Name)
local human = player.Character:WaitForChild("Humanoid")
if stats and human:GetState() == Enum.HumanoidStateType.Seated then
stats.Value = stats.Value + 10
wait(1.99)
end
end
wait(0.01)
end
IsDescendantOf is used to make sure the owner.Value is actually a player of the game, meaning the UserId property in the next condition is valid.
Previous
If you make a table and keep track of "f" for each player then you can change the variable for one player at a time, and not all of them.
13 comments
Do you want every player to sit? If so use a remote event
No they are getting money by sitting onto their chair. But when 2 players sitting at same time and one of them stands up, other one cant get money.
What line isnt working?
Everything is working i didnt understand whats wrong but the last part
Tell me what part so I can help u
You can use print statements to find the part which isnt working
Like i said everything is working correctly but i think ChildAdded and ChildRemoved functions are not the correct function for this script.
try it now, it might not run on nil as nil is not an instance.
to check, the screens change color though, so the issue is just with the while loop? (plz post response under my answer so it shows up in notifications)
Screens arent changing colors and its not giving money. Im trying to understand whats wrong but this is so weird :P
ok, can you try print(player) above the condition in the ChildRemoved and ChildAdded events? when it had the error the player was nil so I want to see if the owner.Value remains nil. If it doesn't you can try printing f[player.UserId] also. plz reply with what prints in your console
Okay, firstly i put them after the local player = owner.Value and it said the name there is no problem. But if i put them after "if Child.Name == "SeatWeld"..." part its not saying anything. That shows if functions have anything wrong.
ok, I updated the answer with a different check. If the player gains points while seated but the screens don't change color, remove the human check and variable for the child added and removed but not the while loop