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

Game not detecting PlayerAdded?

Hey! I've got a problem when trying to run a PlayerAdded function in a Script. It works fine in Studio, but it simply doesn't fire the function in the real game.

Here's my code:

local DescenParent -- I put it here so I can access it from different functions

function PlayerAdded(plr)
	print(1) --I made this to check if the function was fired
	local WorkerDesk = script.Parent:GetDescendants() 
	local Scripts = plr:WaitForChild("Scripts"):WaitForChild("MScripts") --Gets the value of a stat i've done
	wait(0.2)
	for index, descendant in pairs(WorkerDesk) do
		if descendant.Name == "Worker" and Scripts.Value < 1 then --Checks if the descendant is a Worker and if the value is less than 1
			DescenParent = descendant.Parent --sets the DescenParent value to the current descendant parent, so I can return the model to that parent later
			descendant.Parent = game.ServerStorage --moves the models to ServerStorage, making them invisible/untouchable
		end
	end
	Scripts.Changed:Connect(function() --fires when the value is changed
		for index, descendant in pairs(WorkerDesk) do 
			if descendant.Name == "Worker" and Scripts.Value > 0 then --checks if the descendant is a worker and if the player has more than one scripts
				descendant.Parent = DescenParent --gives the models back to their last parent
			end
		end
	end)
end

game.Players.PlayerAdded:Connect(PlayerAdded) --Calls the function

I'm going to explain what I want to do so it is easier for you to understand the code objectives.

It is a game based in Mario 64 where you have to collect Scripts (just like mario stars). This code should make a group of workers (Not NPCs, just models) appear in a specific place when the player has at least 1 script.

--game
	--workspace
		--Desks(folder)
			--MScriptChecker(this script)
			--RandomModel1
			--RandomModel2
			--Worker(model)
			--Worker(model)
	--players
		--RandomPlayer1
			--Scripts(folder)
				--MScripts(NumValue)

I hope someone can help me with all the info I provided. I have been stuck so long in this code. I don't know why it doesn't fire any of the function. Thanks.

3 comments

0

You have no function called PlayerAdded. Instead you should do this :Connect(function(PlayerAdded) and put your script into the function

0

Never mind i say that you called the function know. The text was so tiny

0

you know instead of having to do -- on each line, you could just do --[[ --]]

1 answer

✓ Accepted answer

Ok the problem is that you are trying to access private information of a player. In line 6, you are trying to access MScripts.

local Scripts = plr:WaitForChild("Scripts"):WaitForChild("MScripts")

How Roblox works is that the server can not access the player's individual specific children directly. And the same goes with the player, it can not directly access server things such as ServerStorage. There's basically a big filter blocking the player and server, and the only way to get through that is by using Remote Events/Functions.

I'm not going to explain it all here, but Remote Events allow you to call something to happen from the server, even if it is in a player. I recommend watching some tutorials on this, it's extremely useful in the long run.

1 comment

0

Okk, thanks. I already know how to use remote events/functions. It's just that I thought I could access that folder from a normal script

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