NEED HELP | Another way to PlayerAdded:Connect

Hi, i am searching for change my script.
It works when a player added the game but not ingame when his rebirths count is 1 or 2.

the localsript is in StarterPlayerScripts:

Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player)

local Part = script.Parent
local PlayerStorages = game.ServerStorage:WaitForChild("PlayerStorage")
local plr = PlayerStorages:WaitForChild(player.Name)
local cur = plr:WaitForChild("Currencys")
local rebirth = cur:WaitForChild("Rebirths").Value

local gui = game.StarterGui.ScreenGui.Frame


if rebirth >= 1 then
	gui.Visible = true
	Part.Transparency = 1
	Part.CanCollide = false
end

end)

thx a lot for your help

local Players = game:GetService(“Players”)
local Part = script.Parent
local PlayerStorages = game.ServerStorage:WaitForChild("PlayerStorage")
local plr = PlayerStorages:WaitForChild(player.Name)
local cur = plr:WaitForChild("Currencys")
local rebirth = cur:WaitForChild("Rebirths").Value

local gui = Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").Frame

Players.PlayerAdded:Connect(function()
    while wait(1) do
       if rebirth >= 1 then
	      gui.Visible = true
	      Part.Transparency = 1
	      Part.CanCollide = false
       end
    end
end)

Use while wait() do

While loops are a very bad practice.

2 Likes

The value will always stay the same since it is stored as a variable, make the variable for the instance and use

variable:GetPropertyChangedSignal("Value"):Connect(function()
--if statement here.
end)

my error:

Infinite yield possible on 'ServerStorage:WaitForChild(“PlayerStorage”)

the script:

wait(5)

local plrs = game.Players.LocalPlayer

local serverstorage = game:GetService(“ServerStorage”)

local PlayerStorages = serverstorage:WaitForChild(“PlayerStorage”)

local rebirth = PlayerStorages:FindFirstChild(plrs.Name):WaitForChild(“Currencys”):WaitForChild(“Rebirths”)

local part = game.Workspace.PetGiver.Part

local gui = game.StarterGui.ScreenGui.Frame

function Rebirth()

if rebirth.Value >= 1 then

gui.Visible = true

part.Transparency = 1

part.CanCollide = false

end

end

Rebirth()

plrs:GetPropertyChangedSignal(“Value”):Connect(Rebirth)

Change this , you can’t access the Player’s Gui like that.
You should access it by either PlayerGui or Ancestry [script.Parent.Parent…]

2.You should use Changed event to detect when the rebirth’s value is changed [in addition to the initiate check] , since rebirths is a number value.

3.You can’t access ServerStorage from the client.

this is the script on the part in Workspace:

wait(5)

local plrs = game.Players:GetPlayers()

local serverstorage = game:GetService(“ServerStorage”)

local PlayerStorages = serverstorage:WaitForChild(“PlayerStorage”)

local plr = PlayerStorages:WaitForChild(plrs.Name)

local rebirth = PlayerStorages:WaitForChild(“Currencys”):WaitForChild(“Rebirths”)

local part = game.Workspace.PetGiver.Part

local gui = game.PlayerGui.ScreenGui.Frame

rebirth:GetPropertyChangedSignal(“Value”):Connect(function()

if rebirth.Value >= 1 then

gui.Visible = true

part.Transparency = 1

part.CanCollide = false

end

end)

the error is here:

local plr = PlayerStorages:WaitForChild(plrs.Name) → Argument 1 missing or nil

you can’t view things in ServerStorage using a client (local) script.

Why are you storing a rebirth value in ServerStorage when the client can’t even access it? Here is a basic leaderstats script that saves for your game, put it in ServerScriptService:


local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local playerData = DataStoreService:GetDataStore("PlayerData")


local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local currencyfolder = Instance.new("Folder")
	currencyfolder.Name = "Currencys"
	currencyfolder.Parent = leaderstats
	
	
	local Rebirths = Instance.new("IntValue") --Sets up value for leaderstats
	Rebirths.Name = "Rebirths"
	Rebirths.Parent = currencyfolder

	
	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then
		Rebirths.Value = data['Rebirths']
		

	else
		-- Data store is working, but no current data for this player
		Rebirths.Value = 0
	end
end


local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player:WaitForChild("leaderstats").Currencys:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end

	return player_stats

end


local function onPlayerExit(player)  --Runs when players exit
	
	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)


	if not success then
		warn('Could not save data!')
	end
end

Players.PlayerAdded:Connect(onPlayerJoin)
Players.PlayerRemoving:Connect(onPlayerExit)

And here is your fixed original script:

local Part = script.Parent

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local Gui = PlayerGui:WaitForChild("ScreenGui")

local PlayerStorages = LocalPlayer:WaitForChild("leaderstats")
local cur = PlayerStorages:WaitForChild("Currencys")
local rebirth = cur:WaitForChild("Rebirths")

local Frame = Gui:FindFirstChild("Frame")

rebirth:GetPropertyChangedSignal("Value"):Connect(function(NewValue)
	if NewValue >= 1 then
		Frame.Visible = true
		Part.Transparency = 1
		Part.CanCollide = false
	end
end)

Let me know if you have any questions, thanks!

1 Like

I will try it tommorow. It´s midnight by me (Europe)
but thx for your help.