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

Help with checking a UserId and kicking?

Note: This is in a PlayerAdded function. Note: The UserId is mine. Note: Game is FE.

For some reason, when another player joins, it shows the message but does not kick them, it also does not Destroy the gui.

	if player.UserId ~= 27621739 then
		player:Kick('The game currently is being updated. We are kicking non-developers to prevent you from seeing something unfinished. Check back later, thanks.')
		player.PlayerGui:WaitForChild('Main'):Destroy()
	end

Whole script:

local Data = game:GetService('DataStoreService'):GetDataStore('StatsData')
local Data2 = game:GetService('DataStoreService'):GetDataStore('Data2')

local Prefix = 'user_'
local repStorage = game:GetService('ReplicatedStorage')

--//Remotes
local datastoreRemote = repStorage.DatastoreRemote

game.Players.PlayerAdded:connect(function(player)
	--// Player's Stats
	local Folder = Instance.new('Folder',player)
	Folder.Name = 'Leaderstats'
	local Credits = Instance.new('IntValue',Folder)
	Credits.Name = 'Credits'
	
	if player.UserId ~= 27621739 then
		player:Kick('The game currently is being updated. We are kicking non-developers to prevent you from seeing something unfinished. Check back later, thanks.')
		player.PlayerGui:WaitForChild('Main'):Destroy()
	end
		
	--// Discord API
	if player.UserId ~= 27621739 or player.Name ~= 'Player1' then
		local url = "blehhh"
		local http = game:GetService("HttpService")
		local HookData = {
		['username'] = player.Name,
		['content'] = 'joined Project Shift'
		}
		HookData = http:JSONEncode(HookData)
		http:PostAsync(url, HookData)
	end
	
	--// Loading Data
	local plrData = Data:GetAsync(Prefix  ..tostring(player.UserId))
	if player.UserId == 93794951 then
		if plrData then
			Credits.Value = plrData[1] or 20000
		end
	else
		if plrData then
			Credits.Value = plrData[1] or 0
		end
	end
	
	Credits.Changed:Connect(function()
		local url = "blahhhhckedd"
		local http = game:GetService("HttpService")
		local HookData = {
		['username'] = player.Name,
		['content'] = 'credits changed to: ' ..Credits.Value
		}
		HookData = http:JSONEncode(HookData)
		http:PostAsync(url, HookData)
	end)
end)

--// RemoteEvent(Saving Car that player picked)
datastoreRemote.OnServerEvent:Connect(function(player,carPicked)
	Data2:SetAsync(Prefix  ..tostring(player.UserId),{carPicked})
end)

game.Players.PlayerRemoving:connect(function(player)
	local Credits = player.Leaderstats.Credits
	
	--// Saving Data
	Data:SetAsync(Prefix..tostring(player.UserId), {player.Leaderstats.Credits.Value})
end)

2 comments

1

Can we see the entire script? Or whichever part pertains to this?

0

Sure

1 answer

The problem could be that the PlayerAdded function doesn't run for players that join before the PlayerAdded. Try this script?

--//@author FiredDusk

--//Services
local Players = game:GetService('Players')
local HttpService = game:GetService('HttpService')
local DataStoreService = game:GetService('DataStoreService')
local ReplicatedStorage = game:GetService('ReplicatedStorage')

--//DataStores
local Data = DataStoreService:GetDataStore('StatsData')
local Data2 = DataStoreService:GetDataStore('Data2')

--//Remotes
local DatastoreRemote = ReplicatedStorage.DatastoreRemote

local function PlayerAdded(Player)
	print("PlayerAdded function running for", Player.Name)
	
	--// Player's Stats
	local Folder = Instance.new('Folder', Player)
	local Credits = Instance.new('IntValue', Folder)

	local Name = Player.Name
	local UserId = Player.UserId
	
	Folder.Name = 'Leaderstats'	
	Credits.Name = 'Credits'

	if UserId ~= 27621739 then
		Player:Kick('This game currently is being updated. Non-developers are being kicked for the time being until the polished version is available. Please return later :D')
		Player.PlayerGui:WaitForChild('Main'):Destroy()
	end

	--// Discord API
	if UserId ~= 27621739 or Name ~= 'Player1' then
		local Url = 'blehhh'
		
		HttpService:PostAsync(Url, HttpService:JSONEncode{
			username = Name;
			content = 'joined Project Shift';
		})
	end

	--// Loading Data
	local PlrData = Data:GetAsync(('user_%d'):format(UserId))
	Credits.Value = PlrData and PlrData[1] or (UserId == 93794951 and 20000) or 0

	Credits.Changed:Connect(function()
		HttpService:PostAsync('blahhhhckedd', HttpService:JSONEncode{
			username = Name;
			content = ('credits changed to %d'):format(Credits.Value);
		})
	end)
end

local Playerlist = Players:GetPlayers()
game.Players.PlayerAdded:Connect(PlayerAdded)

for a = 1, #Playerlist do
	PlayerAdded(Playerlist[a])
end

--// Data Saving
DatastoreRemote.OnServerEvent:Connect(function(Player, CarPicked)
	--//Save Car that players pick
	Data2:SetAsync(('user_%d'):format(UserId), {CarPicked})
end)

game.Players.PlayerRemoving:Connect(function(Player)
	Data:SetAsync(('user_%d'):format(UserId), {Player.Leaderstats.Credits.Value})
end)

2 comments

0

This is not a local script.

0

^ Who said it was?

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