How to find player from string

local gamepassId = "20537086" -- Specify your gamepass ID here
local remote = game.ReplicatedStorage:FindFirstChild("BucksR")
-- Create TextButton
local button = script.Parent

-- Function to handle button click
local function onButtonClicked()
	-- Check if player already owns the gamepass
	game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer, tonumber(gamepassId))
end

-- Connect button click event
button.MouseButton1Click:Connect(onButtonClicked)

-- Handle gamepass purchase finished event
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, pass_id, was_purchased)
	if was_purchased then
		-- Create the frame and list layout once
		local screengui = Instance.new("ScreenGui")
		screengui.Parent = script.Parent.Parent

		local frame = Instance.new("Frame")
		frame.Size = UDim2.new(0, 200, 0, 300)
		frame.Position = UDim2.new(0.472, 0, 0.468, 0)
		frame.BackgroundTransparency = 0.5
		frame.BackgroundColor3 = Color3.new(0, 0, 0)
		frame.Parent = screengui

		local listLayout = Instance.new("UIListLayout")
		listLayout.Parent = frame
		listLayout.Padding = UDim.new(0, 5)
		listLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
		listLayout.VerticalAlignment = Enum.VerticalAlignment.Top

		-- Function to update player list
		local function onPlayerNameClicked(playerName)
			remote:FireServer(playerName)
			-- Fire the remote event with the player's name
			print(playerName)
		end

		-- Display player names in separate TextButtons
		local players = game:GetService("Players"):GetPlayers()
		for _, player in ipairs(players) do
			local nameLabel = Instance.new("TextButton")
			nameLabel.Size = UDim2.new(1, 0, 0, 20)
			nameLabel.BackgroundTransparency = 1
			nameLabel.TextColor3 = Color3.new(1, 1, 1)
			nameLabel.TextSize = 14
			nameLabel.Text = player.Name
			nameLabel.Parent = frame
			nameLabel.MouseButton1Click:Connect(function()
				onPlayerNameClicked(player.Name)
				nameLabel.Parent:Destroy()
				return
			end)
		end
	end
end)
local remote = game.ReplicatedStorage:WaitForChild("BucksR")

remote.OnServerEvent:Connect(function(name)
	print("RECEIVED")
	local nameofplayer = name
	local player = game:GetService("Players"):FindFirstChild(name)
	if player then
		player.leaderstats.Bucks.Value += 100
		print("receive bucks")
	else
		print("no player bro")
	end
end)

prints “no player bro”
is there any issue in my code or am i not doing something right? its like a donate script and i need help

the first argument in OnServerEvent is always the player that fired the remote event.

replace your serverscript with this:

local remote = game.ReplicatedStorage:WaitForChild("BucksR")

remote.OnServerEvent:Connect(function(RemoteCaller, name)
	print("RECEIVED")
	local nameofplayer = name
	local player = game:GetService("Players"):FindFirstChild(name)
	if player then
		player.leaderstats.Bucks.Value += 100
		print("receive bucks")
	else
		print("no player bro")
	end
end)

also i would recommend checking if the player bought the gamepass on the server because exploiters can just fire the remote giving everyone bucks for free

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.