Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script is fully correct but how do i fix this?

Everything is completely correct, but if one player gets up while two players are sitting on their computer in their tycoon, the other one's (who's sitting on computer) function is not working.

local f = true
local screenn = script.Parent.Parent.Parent.monitor.screen

script.Parent.ChildAdded:Connect(function(Child)
	if Child.Name == "SeatWeld" and f == true then
		f = false
		screenn.BrickColor = BrickColor.new("Steel blue")
		local player = game:GetService("Players")
		while true do
			for _,i in pairs(player:GetPlayers()) do
				if script.Parent.Parent.Parent.Parent.Parent.Owner.Value == i and f == false then
					local stats = game.ServerStorage.PlayerMoney:FindFirstChild(i.Name)
					stats.Value = stats.Value + 10
					wait(2)
				end
			end
		end
	end
end)


script.Parent.ChildRemoved:Connect(function(Child)
	if Child.Name == "SeatWeld" and f == false then
		f = true
		screenn.BrickColor = BrickColor.new("Black")
	end
end)

(Script is server type script, not local. Do i have to make it local?)

13 comments

0

Do you want every player to sit? If so use a remote event

0

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.

0

What line isnt working?

0

Everything is working i didnt understand whats wrong but the last part

0

Tell me what part so I can help u

0

You can use print statements to find the part which isnt working

0

Like i said everything is working correctly but i think ChildAdded and ChildRemoved functions are not the correct function for this script.

0

try it now, it might not run on nil as nil is not an instance.

0

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)

0

Screens arent changing colors and its not giving money. Im trying to understand whats wrong but this is so weird :P

0

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

0

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.

0

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

1 answer

✓ 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.

4 comments

0

It says index nil with 'IsDescendantOf' on 32. line

0

Now there is no error but its not giving money. I got really bored im just trying to do this for 1 week and nothing working.

0

Okay! I just found which one is wrong. The f[Player.UserId] ones are broken. I just deleted them and played game and its worked. But there is also a problem with while true do. Player gets money without sitting on the chair, it has to get it when he's sitting on it. (Btw screens color are changed the real problem is f[player.UserId])

1

Okay, everything is fixed. Screens wasnt changing colors and i did what you said and its fixed too. Thanks for all your help, have a great day!

This discussion is preserved as it was. New answers, comments, and votes are closed.