Is there a way to path to the player backpack with a server script?

Hello. I’m trying to make a script that counts down, chooses a random number, checks the player backpack for a tool that has a TextLabel inside of it and then reads the text. Issue is I don’t really know how to path my way into the backpack since I’m inside of a serverscript and the countdown and randomizer has to be server sided.
Here’s my code

local WinningNumber = script.Parent.SurfaceGui.WinningNumber
local RoundTimeCountDown = script.Parent.SurfaceGui.RoundTimeCountdown
local Players = game:GetService("Players")


while true do
	for i = 15, 0, -1 do
		RoundTimeCountDown.Text = i
		task.wait(1)
	
		if i == 0 then
			local number = math.random(1,100)
			WinningNumber.Text = number
			i = 15
			
		end
	end
end

Any help/tips are greatly appreciated.

3 Likes

you can do this:

local Players = game:GetService("Players")

for _, Player in pairs(Players:GetPlayers())
   local Backpack = Player:FindFirstChild("Backpack")
   -- now you can check
end
3 Likes

Thank you for your answer. I added the rest of the script but something is wrong. Am I not properly pathing the card? It doesn’t print the print function I added.

local WinningNumber = script.Parent.SurfaceGui.WinningNumber
local RoundTimeCountDown = script.Parent.SurfaceGui.RoundTimeCountdown
local Players = game:GetService("Players")


while true do
	for i = 15, 0, -1 do
		RoundTimeCountDown.Text = i
		task.wait(1)
	
		if i == 0 then
			local number = math.random(1,1)
			WinningNumber.Text = number
			i = 15
			for _, Player in pairs(Players:GetPlayers()) do
				local Backpack = Player:FindFirstChild("Backpack")
				if Backpack:FindFirstChild("Lottery Card") then
					local cardText = Player.Backpack:FindFirstChild("Lottery Card").LotteryCard.SurfaceGui.TextLabel
					local card = Player.Backpack:FindFirstChild("Lottery Card")
					if cardText.Text == number then
						print("YOU WON!!!!!!!!!!!!!!")
						card:Destroy()
					end
				end
			end
		end
	end
end

image
(this gets cloned into the backpack)

3 Likes

print cardText.Text and print number, let me know what it says. also you should use the Backpack variable, instead of repathing the Backpack.

1 Like

the card text doesn’t equal to the number even when i set it to 1 and the math.random chooses 1, it also only prints when the tool is unequipped

2 Likes

when the tool is equipped, it goes to the character model instead. so you can do this:

local Character = Player.Character or Player.CharacterAdded:Wait()
local lotteryCard = Backpack:FindFirstChild("Lottery Card") or Character:FindFirstChild("Lottery Card")
local cardText = lotteryCard.LotteryCard.SurfaceGui.TextLabel 

if cardText.Text == number then
	print("YOU WON!!!!!!!!!!!!!!")
	lotteryCard:Destroy()
end
4 Likes

can you show me how you’re setting the card text to 1? are you doing it through script or by studio

1 Like

I set the math.random to (1,1) from (1,100)

1 Like

how are you setting the card text to 1, not the random number, because you’re setting the card text to 1 and its not replicating.

1 Like

I’m using a TextBox and just setting the number to 1.

1 Like

oh I see, do this instead, string is not equal to a number:

local Character = Player.Character or Player.CharacterAdded:Wait()
local lotteryCard = Backpack:FindFirstChild("Lottery Card") or Character:FindFirstChild("Lottery Card")
local cardText = lotteryCard.LotteryCard.SurfaceGui.TextLabel 

if tonumber(cardText.Text) == number then
	print("YOU WON!!!!!!!!!!!!!!")
	lotteryCard:Destroy()
end
2 Likes

Thank you so much. Have a good day.

2 Likes

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