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

Why can't I access the value in the players folder?

it is a server script and when I try to get the value it comes up with the error ServerScriptService.Gravel.Script:4: attempt to index nil with 'DataFolder'

Gravel = workspace.Things.gravel

function gravel(player)
	local DataStore = player.DataFolder.GravelColor --Line with error
	while true do
		if Gravel.BrickColor == "Cyan" then
			DataStore.Value = 3
		end
		if Gravel.BrickColor == "Really Red" then
			DataStore.Value = 1
		end
		if Gravel.BrickColor == "Buttermilk" then
			DataStore.Value = 2
		end
		wait(.2)
	end
end

while true do
	gravel(game.Players.LocalPlayer)
end

2 comments

0

game.Players.LocalPlayer is nil on the server for obvious reason - it's not a client, server does not know who you want to specify. I also don't understand point of while loop on line 19, why when you have another one one in the function gravel.

0

I was just changing things around to see what would work and I forgot I had a while loop in the function and also that is why I have the local Player thing

1 answer

✓ Accepted answer

Like what @imKirda said, you can't define a LocalPlayer through a server script. You should connect a function to update when the BrickColor of Gravel changes instead of creating a loop to check for changes. Also, I'm quite confused. When the gravel's BrickColor changes, it updates the DataStore value to all players? Nonetheless, I'll try my best to get what you are doing.

local gravel = workspace.Things.gravel

-- connect event to listen for changes
gravel.Changed:Connect(function()
	-- loop through all players
	for _, player in pairs((game.Players:GetPlayers())) do
		local dataStore = player.DataFolder.GravelColor
		
		if gravel.BrickColor == BrickColor.new("Really red") then
			dataStore.Value = 1
		elseif  gravel.BrickColor == BrickColor.new("Buttermilk") then
			dataStore.Value = 2
		elseif gravel.BrickColor == BrickColor.new("Cyan") then
			dataStore.Value = 3
		end
		
	end
end)

Hope this is what you are going for. If not, do let me know and explain clearly. Hope this helps!

8 comments

0

it is suposed to update for 1 player but it is a 1 player server so i guess that would probably be the same.

0

Yeah that would still work. Remember to accept though! :D

0

i just tried and it still didnt change the value... is this one of those things that only works in actual roblox?

0

How are you changing the value? Are you changing through a client side? It only works when the server changes the value since we are using a server script.

0

i check through players yasquerda datafolder gravelcolor and it stays at 1 no matter what color it is so i don't know if that is client side or server

0

The gravel's brickcolor has to be changed by another server script.

0

Ok now i changed it to a server script changing it's color and it works Thanks

0

Happy to help! :D

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