New scripter trying to learn FE

Hey guys, I`m VERY new to coding and I’ve come across a mistake that I cannot figure out…

Currently, in my “ServerScriptService” I have the following code inside of a server script -

game.Players.PlayerAdded:Connect(function(plr) -- Appears when they join

game.ReplicatedStorage.GUI.Activate_Start:FireAllClients() --GUI is just a folder, Activate_Start is a remote event

print("Fired")

end)

And that’s all fine and dandy, when I click play, it prints “Fired”, however, It’s the next part that I am stuck on.

    local player = game.Players.LocalPlayer

    game.ReplicatedStorage.GUI.Activate_Start.OnClientEvent:Connect(function(thing)

    print("caught")

    if player.PlayerGui:WaitForChild("Press_It") then

    print("hi")

    player.PlayerGui:FindFirstChild("Press_It").Frame:TweenPosition(UDim2.new(0.166,0,.767,0),"In","Elastic",5)



    wait(4)

    player.PlayerGui:FindFirstChild("Press_It").Value.Value = true

    

    end

    end)

It does not print “Caught” and does not activate the script. This is a local script… What have I done wrong? thanks!

-Revelted

EDIT: The local script is in “StarterPlayerScripts” – Sorry

3 Likes

This is most likely because the player has not loaded (has not started running their scripts yet) when the PlayerAdded event gets fired. You’re not giving the player time to run their scripts.

To fix this, you could either make the client tell the server when it’s ready to receive events (and then fire your remote after that), or just do a fixed wait when the player is added (which won’t guarantee the client is ready). Preferably do the former:

Server

RemoteEvent.OnServerEvent:Connect(function(Player)
    -- When the player is ready to receive the event, fire it
    RemoteEvent:FireClient(Player)
end)

Client

RemoteEvent.OnClientEvent:Connect(function()
    -- Your code
end)

-- Let the server know the client is ready to receive the event
RemoteEvent:FireServer()

However, note that since this is firing when the player joins, why not just make the client script do it’s thing straight away without waiting for the server?

FE is for stopping exploiters from ruining other’s experiences and cheating the game, not for obsessively verifying that everything is going exactly as it should. The client can handle things like loading screens and menus perfectly fine. Just ask yourself the question: Would exploiting this feature be worthwhile? :slightly_smiling_face:

3 Likes

I tried just having the client doing all the work, but it doesnt work q.q I think theres something wrong with the tween script but I’m not to sure.

I tried both of your ideas and I still have no avail :frowning:

Am I able to have a PlayerAdded local script in the “StartPlayerScripts” folder?

Not even this works in a local script and im so confused rn >.<

game.Players.PlayerAdded:Connect(function(plr)

wait(1)

print(plr)

end)

This code doesn’t work in Studio’s play solo mode because the player is created before scripts that connect to PlayerAdded run. Also, if you connect to this event in a localscript, you will not see this event fired for the localscript’s client joining, likely for a similar reason as above - the player is created (added) before its script that connects to the event start running.

Also I recommend this article. It’s important to indent your code.


Overall I’m not quite sure what you’re trying to accomplish by firing a remote on all clients when a player joins. You could skip the remote completely and simply connect to PlayerAdded on the client. You should be careful not to overuse remotes. Always consider whether or not you need to use a remote at all - read up on the client-server model.

I got rid of the remote event, I just want a simple task to be done - a nice tween when the player enters the game, however, anything I try doesn’t work, I just Tried using remote events because I thought it would help, but it didn’t.

I also ran this under a real server and it still doesn’t work >.< I don’t know how to make a function activate in the client when the client joins the game… I’ve added waits everywhere and still no luck. I know how to clean up my code, I just get lazy when I’m copying and pasting onto the wiki.

Please don’t be lazy when you’re trying to get help. It helps us understand you better.


Could you describe more in detail what you’re actually trying to make happen and when?

You could simply have a LocalScript in ReplicatedFirst. LocalScripts in ReplicatedFirst will run as early as possible. Typically this is used for loading screens. If you don’t need it to run so early, StarterPlayerScripts is also a good place for running code once, when the player joins.

I managed to get what I wanted by doing this:

local player = game.Players.LocalPlayer
wait(.01)
if game.Players:WaitForChild(player.Name).Backpack then

	player.PlayerGui:FindFirstChild("Press_It").Frame:TweenPosition(UDim2.new(0.166,0,.767,0),"In","Linear",3)
end

If this helps any, I don’t know if this is a proper way to do this, but it works.

Well you already have the local player, just call :WaitForChild(“Backpack”) instead of re-grabbing the player from the Players Service and accessing the backpack that way.

Oh right. lol

I would also remove the wait, you should be waiting for PlayerGui to load first, then your screen gui, then your Press_It object. So player:WaitForChild(“PlayerGui”) and etc. Additionally, you do not need to wait for Backpack at all since you aren’t using it.

Alright, I also have one more question - Whats the proper way to change a players camera angle?

right now I have

local Camera = game.Players.LocalPlayer.Camera
in a local script, however I don’t think this is correct…i’m looking for a wiki for this but i cant find one, could you possibly link one or tell me how to get the camera for a local player ;o

Getting the camera is actually fairly easy, you could directly access the camera object in workspace (not recommended) or you could access it using workspace.CurrentCamera .
To change the camera angle you should look into CFrame’s.
A quick explanation of how rotations work with CFrames is that for say you have a CFrame with 0 rotation on each axis. If you were to multiply that CFrame by CFrame.fromOrientation(0,math.pi/2,0) it would add 90 degrees (pi/2 radians) to the y axis. That’s how you can view rotations with CFrames, it’s simple addition.

To make the camera follow a certain block , do I need to use a loop?

Yes

Finally made what I was planning on lol thanks guys

1 Like