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