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

Why won't this search through the players then print?

Gui that when button is pressed runs server script My Localscript

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEvents.AddBounty:FireServer()
end)

My ServerScript

--Check Players
game.ReplicatedStorage.RemoteEvents.AddBounty.OnServerEvent:Connect(function()
	local Players = game:GetService("Players")
	local textbox = script.Parent.Parent.StarterGui.BountyGUI.Frame.SearchBox

	 for _, v in pairs(Players:GetPlayers()) do 

        if textbox.Text == v.Name then 
           
           print(v.Name.."Was found")
        end
    end
end)
	


if the searchbox.text is equal to a player's name shouldn't it print? I'm so confused.

UPDATE: Local

local Frame = script.Parent.Parent.Parent:WaitForChild('Frame')
local SearchBox = Frame:WaitForChild('SearchBox')
local ValueBox = Frame:WaitForChild('ValueBox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')
local AddBountyEvent = RemoteEvents:WaitForChild('AddBounty')

script.Parent.MouseButton1Click:Connect(function()
    AddBountyEvent:FireServer(SearchBox.Text, ValueBox.Text)
end)

Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')
local AddBountyEvent = RemoteEvents:WaitForChild('AddBounty')

AddBountyEvent.OnServerEvent:Connect(function(PlayerWhoClicked, searchname, valueAmount)
    local PlayerInPlayers = Players:FindFirstChild ( searchname )
	local ValueOfBounty = Players:FindFirstChild ( valueAmount )
if PlayerWhoClicked.leaderstats.Money.Value >= valueAmount then
	PlayerWhoClicked.leaderstats.Money.Value = PlayerWhoClicked.leaderstats.Money.Value - valueAmount
	PlayerInPlayers.leaderstats.Bounty.Value = PlayerInPlayers.leaderstats.Bounty.Value + valueAmount
else if PlayerWhoClicked.leaderstats.Money.Value < valueAmount then
	end
end
   
end)

2 comments

0

Does it give you any errors?

0

no

1 answer

✓ Accepted answer

I'm guessing your button is the enter button after a user fills in a username? In which case, you can send the content of the TextBox. You also shouldn't be trying to reference anything in StarterGUI. You place GUI's in StarterGUI that will be replicated to players. Whatever you want to change needs to be in the player's PlayerGui to see it live.

The first Parameter sent with a RemoteEvent is the local player that called it. Additional code added due to additional request. There are likely errors, I didn't test it, monitor the output.

LocalScript

-- Parent to the correct location
local Frame = script.Parent.Parent.Parent:WaitForChild('Frame')
local SearchBox = Frame:WaitForChild('SearchBox')
local ValueBox = Frame:WaitForChild('ValueBox')

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')
local AddBountyEvent = RemoteEvents:WaitForChild('AddBounty')

script.Parent.MouseButton1Click:Connect(function()
    -- If there's text in the box and it contains at least one number
        -- Can be more strict, look into regex
    local IsNumber = SearchBox.Text:len() > 0 and ValueBox.Text:match('%d+') or false
    if IsNumber then 
        AddBountyEvent:FireServer(SearchBox.Text, tonumber ( IsNumber ) )
    end
end)

Script

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local RemoteEvents = ReplicatedStorage:WaitForChild('RemoteEvents')
local AddBountyEvent = RemoteEvents:WaitForChild('AddBounty')

AddBountyEvent.OnServerEvent:Connect(function(PlayerWhoClicked, searchname, cost)
    local PlayerInPlayers = Players:FindFirstChild ( searchname )
	if PlayerInPlayers then
		local PlayerWhoClickedLeaderStats = PlayerWhoClicked:FindFirstChild("leaderstats")
		local PlayerInPlayersLeaderStats = PlayerInPlayers:FindFirstChild("leaderstats")
		local Money = PlayerWhoClickedLeaderStats and PlayerWhoClickedLeaderStats:FindFirstChild("Money") or false
		local Bounty = PlayerInPlayersLeaderStats and PlayerInPlayersLeaderStats:FindFirstChild("Bounty") or false
		if Money then 
			local HasEnough = Money.Value >= cost
			local Difference = cost - Money.Value
			if HasEnough then
				Money.Value = Money.Value - cost
				if Bounty then 
					Bounty.Value = Bounty.Value + cost
				end
			else
				--Format is cool, check it out
				print ( ('%s needs %d/%d more cash to set bounty'):format(PlayerWhoClicked.Name, Difference, cost)  )
			end
		end
	else 
		print ( 'username not found' )
	end
end)

6 comments

0

Question, Where is searchname coming from? is that equivalent to SearchBox.Text in the LS?

0

Yes, searchname is SearchBox.Text

0

Yes I understand that, but in the script at line 6, (PlayerWhoClicked, searchname) what is searchname? SearchBox.Text? I'm trying to a value box in the same manner I messaged you on RBLX. Will compensate in ROBUX/Cash if need be.

0

paypal? I'll be needing you in the future 100% lol.

0

After weeding out most the errors it comes to a point where it simply skips the ternary operation and just prints.

0

invite me to team create

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