Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can't get CharacterAdded or PlayerAdded to work?

Asked by 2 years ago

local afkStatus = game.Players.LocalPlayer:WaitForChild("AFKToggle") local plrInRoundStatus = game.Players.LocalPlayer:WaitForChild("PlayerInRoundStatus")

This is a Server Script in ServerScriptStorage. Basically what is happening is I have two Bool Values being added from a Local Script to the Local Player, and in the server script I am try to find get those for use in that COde. The thing is, the Server Script obviously runs before the Local Script. I've tried PlayerAdded and CharacterAdded countless times but can't get them to work.

1
Local Scripts = Client Sided Only - If you are adding stuff into the client without remotes; The server cannot pick these up. xXTouchOfFrostXx 125 — 2y
1
LocalPlayer cannot be called via ServerScripts only LocalScripts xXTouchOfFrostXx 125 — 2y
0
So what would I do? Make a remote event to grab this stuff? Or make a Remote Event to Create the Instances? XRangerGuyX 29 — 2y
0
Also, I tried adding the Instances in the Server Script, but yet again the Character wasn't loaded, so the script was giving Errors. If I was going to do it through the Server Script, how would I make it wait for the player to be loaded in? XRangerGuyX 29 — 2y
View all comments (3 more)
0
This shows how to wait for the player as well as documentation for the character: https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded xXTouchOfFrostXx 125 — 2y
0
IMO - For your system; Place the BoolValues into the Player not the character as the player will only lose the values if they disconnect or if they are removed by another script - Where as character dies each time causing the values to be removed. xXTouchOfFrostXx 125 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Server Script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

    --Creating the BoolValues

    local afkStatus = Instance,new("BoolValue") -- Value is false by default
    afkStauts.Name = "AFKToggle"

    local plrInRoundStatus = Instance,new("BoolValue") -- Value is false by default
    plrInRoundStatus.Name = "PlayerInRoundStatus" 

    --Lets create a folder to keep these safe

    local boolFolder = Instance.new("Folder",player)
    boolFolder.Name = "boolFolder"

    afkStatus.Parent = boolFolder
    plrInRoundStatus.Parent = boolFolder

    --If you wish to access their values;
    print(player.boolFolder:FindFirstChild(afkStauts).Value
    --false will print due to their values automatically being set to false.
end)

Hope this helps.

Don't forget, leave an up-vote if this helps.

0
So is my entire script gonna have to be in that Event? Because I can't use the 'player' parameter outside of the Event.. XRangerGuyX 29 — 2y
0
@XRangerGuyX can you please explain more on what you mean? JesseSong 3916 — 2y
0
Ranger - This is a script not an event; Events are not needed here. If you are accessing the varaibles outside of this script you require it as so: game.Players.PLAYERNAME.boolFolder.AFKToggleORPlayerInRoundStatus.Value = true/false xXTouchOfFrostXx 125 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

(This is an answer to RangerGuy's question in the comments, not to the main question)

Anything that needs to run when the player is added does need to be in the PlayerAdded event handler. If you need to access the player in a different event, you'll need to store data in a table (with the player as the key of that table). You can also call other functions from within that event handler and pass the player, if doing so helps organization. Examples:

local Players = game:GetService("Players")

local function doSomething(player)
    -- can put in code that uses 'player' here
end

local playerData = {}
Players.PlayerAdded:Connect(function(player)
    local data = {
        Money = 5, -- for instance
    }
    playerData[player] = data
end)

-- Now when some event happens (perhaps a RemoteEvent), you can access playerData:
local function remoteEventHandler(player)
    local data = playerData[player]
    -- can do something with data here
end

-- Just remember when storing data per player to remove the data when they leave or else you'll end up with a memory leak:
Players.PlayerRemoving:Connect(function(player)
    playerData[player] = nil
end)

-- However, if you don't have any data you want to store (for instance, because you're using BoolValues and IntValues and so on), you don't need to have the playerData table at all, you just need to iterate over the players when you want to do something for each player:
for _, player in ipairs(Players:GetPlayers()) do
    if player.Money.Value >= 5 then -- pretending you have an IntValue named Money and wanted to print out all players who have at least 5
        print(player, "has", player.Money.Value, "money")
    end
end
-- Note that you would want that 'for' loop inside a function that is run in response to an event or else you'll run the loop when the script starts, which in this case would occur before the server has any players

Answer this question