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

What function would I use to access a value from another player?

I have a button that allows you to challenge a player that you want to play against. Everything works fine, but I want to add a function where if the player you want to battle already has a request from someone else, the RequestRemote wont fire and give the player an error message. I just don't know what function can accomplish this because the Boolean value has to be accessed from the localscript.

The boolean "hasRequest" is created in everyone's Player. This is the value I want to check before sending a request.

Here is my LocalScript for reference:

local id = script.Parent.Parent:WaitForChild("PlayerID")
local text = script.Parent:WaitForChild("Battletext")
local Request = game.ReplicatedStorage.DefaultRequest
local localgui = game:GetService('Players').LocalPlayer:WaitForChild("PlayerGui")
local localplayerid = game.Players.LocalPlayer.UserId
local lookfor = game.ReplicatedStorage.BattleEvent
local lookForGameNextClick = true
local player = game:GetService("Players"):GetPlayerByUserId(id.Value)
local localname = game.Players.LocalPlayer.Name
local iswaiting = game:GetService('Players').LocalPlayer:WaitForChild("WaitingForPlayer")
local hasgui = game:GetService('Players').LocalPlayer:WaitForChild("hasRequest")
 
 

script.Parent.MouseButton1Click:connect(function()
 	--print(id.Value)
	--print(localplayerid)
	
	if id.Value == localplayerid then -- can't battle yourself in the list
		script.Disabled = true
		text.Text = "Can't Do That"
		wait(2)
		script.Disabled = false
		text.Text = "BATTLE"
		
	else
		if hasgui.Value == true then
			print("True")
			text.Text = "Waiting!" -- waiting for player to accept or decline
			script.Disabled = true
			wait(2)
			text.Text = "BATTLE"
			script.Disabled = false
			return
		end
		if hasgui.Value == false then -- if localplayer has a request, another request can't be sent.
			print("False")
			if iswaiting.Value == false then
				text.Text = "Cancel"
				game.ReplicatedStorage.BoolTrueRemote:FireServer(localname) 
				sendmessage()
				return
			end
			if iswaiting.Value == true then
				text.Text = "Waiting!" -- waiting for player to accept or decline
				script.Disabled = true
				wait(2)
				text.Text = "BATTLE"
				script.Disabled = false
				return
			end
		end
	end
end)

function sendmessage() -- sends the request to player.
	game.ReplicatedStorage.RequestRemote:FireServer(player)
end

ServerScript

local changeRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
changeRemote.Name = "BoolTrueRemote"

local change2Remote = Instance.new("RemoteEvent", game.ReplicatedStorage)
change2Remote.Name = "BoolFalseRemote"

local declineRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
declineRemote.Name = "DeclineRemote"

local Decline = game.ReplicatedStorage.defaultDecline

local requestRemote = Instance.new("RemoteEvent", game.ReplicatedStorage)
requestRemote.Name = "RequestRemote"

local Request = game.ReplicatedStorage.DefaultRequest

local HasGui = Instance.new("RemoteEvent",game.ReplicatedStorage)
HasGui.Name = "GuiTrueRemote"

local HasNotGui = Instance.new("RemoteEvent",game.ReplicatedStorage)
HasGui.Name = "GuiFalseRemote"

requestRemote.OnServerEvent:Connect(function(plr, plr2)
    local msg = Request:Clone()
	local text = Instance.new("TextLabel")
	local playername = Instance.new("IntValue")
	playername.Value = plr.UserId
	playername.Name = "PlayerID"
	local box = msg:WaitForChild("MovingBox")
	text.Text = plr.Name .. " wants to battle you!"
	text.Size = UDim2.new(0.7,0,1,0)
	text.Position = UDim2.new(0,0,0,0)
	text.BackgroundTransparency = 1
	text.TextScaled = true
	text.TextWrapped = true
	print(plr)
	print(plr2)
	print(text.Text)
    msg.Parent = plr2:WaitForChild("PlayerGui")
	text.Parent = box
	playername.Parent = box
	
end)

declineRemote.OnServerEvent:Connect(function(plr, plr2)
    local msgs2 = Decline:Clone()
	print(plr)
	print(plr2)
    msgs2.Parent = plr2:WaitForChild("PlayerGui")
	local boolval = plr2:WaitForChild("WaitingForPlayer")
	boolval.Value = false
	
end)

changeRemote.OnServerEvent:Connect(function(plr)
   
	local boolvalue = plr:WaitForChild("WaitingForPlayer")
	boolvalue.Value = true
	
end)

change2Remote.OnServerEvent:Connect(function(plr)
   
	local boolvalue = plr:WaitForChild("WaitingForPlayer")
	boolvalue.Value = false
	
end)

HasGui.OnServerEvent:Connect(function(plr)
   
	local guiE = plr:WaitForChild("hasRequest")
	guiE.Value = true
	
end)

HasNotGui.OnServerEvent:Connect(function(plr)
   
	local guiE2 = plr:WaitForChild("hasRequest")
	guiE2.Value = false
	
end)

game.Players.PlayerAdded:connect(function(player)
	
	local bool = Instance.new("BoolValue", player)
	bool.Value = false
	bool.Name = "WaitingForPlayer"
	
	local bool2 = Instance.new("BoolValue", player)
	bool2.Value = false
	bool2.Name = "hasRequest"
end)

2 comments

0

Please post the server script code.

0

Okay

1 answer

✓ Accepted answer

Clone a local script to the player that they are trying to request to battle with and check if they are battling or have a current request. Then fire a remote event to communicate with the server and then communicate back to the player sending the request.

1 comment

0

Alright, I think I know what to do now.

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