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

How can I include a changing variable in a GUI thats the same for everyone?

So basically I have a finish line (a no collide, transparent wall), when someone crosses this finish line a GUI should pop up telling them what place they're in. To achieve this I've used a Script to to launch a remote event when someone touches the block and a local script to make that GUI visible. My problem is getting that variable in the TextBox of the GUI. Basically I want a variable that goes up by one every time someone touches the block so that the GUI can tell someone what place they got in a race.

Here's my Script:

local debounce = false

game.Workspace.FinishLine.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			
			game.ReplicatedStorage.ShowGUI:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))
			
		end
		wait(2)
		debounce = false
	end
end)

and here's my local script:

game.ReplicatedStorage.ShowGUI.OnClientEvent:Connect(function()	
	script.Parent.Frame.Visible = true
	wait(2)
	script.Parent.Frame.Visible = false
end)

5 comments

0

Are you trying to make the ui pop up to everyone or just a single person?

0

Just a single person. So the first person would touch the block and only they would get the message saying "You're 1st" and the second person to touch the block would get the pop up "You're 2nd" when they touch the block. But I need the placement variable to go up by one each time so everyone gets the right placement number.

0

I think it's better to use Remote Function, because you can communicate between Server-Client-Server!

0

Alright, I did look into that a bit, and figured it could be the solution. If I went that route could I then create a function in one script that only applies to a local player?

0

Yeah I think you can

1 answer

First, if you want to make a value/variable in client, don't do it! Your client can manipulate the value and this is not good if your game has a reward system.

So you want to use Remote Function

Basically, in your server script above you just want to change Remote Events to Remote Functions.

You want to create an int value inside ReplicatedStorage since it's the much more safer than using value inside the client.

Main Variable: (1) ShowGUI ( Remote Function inside RS ) (2) ShowRanking ( Remote Events inside RS ) (3) Placement ( Int Value inside RS )

local RS = game:GetService("ReplicatedStorage")
local Rank = RS:WaitForChild("Placement") --create an int value inside RS with value of 0

local debounce = false

game.Workspace.FinishLine.Touched:Connect(function(hit)
	if not debounce then
		debounce = true
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local Result = game.ReplicatedStorage.ShowGUI:InvokeClient(player))
		end
	end
wait(.25)
debounce = false
end)

Now in your local script, you want to pick up the information from the server and then return a boolean value

local Event = game:GetService("ReplicatedStorage").ShowGUI

Event.OnClientInvoke = function()
	return true
end

After that basically the server will get the result of true. Simply say this inside your server script under Result Variable

if Result == true then
	Rank.Value = Rank.Value + 1
	game.ReplicatedStorage.ShowRanking:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent), Rank.Value) -- create a new remove event
end

Then, the client will pick it up again and you are ready to go, simply set the text to whatever you want, I'll give an example:

game.ReplicatedStorage.ShowRanking.OnClientEvent:Connect(function(playerRank)
	local rank = tonumber(playerRank)
	print(rank)
	script.Parent.Visible = true
	if rank == 1 then
		script.Parent.Text = "You are " .. rank .. "st!"
	elseif rank == 2 then
		script.Parent.Text = "You are " .. rank .. "nd!"
	elseif rank == 3 then
		script.Parent.Text = "You are " .. rank .. "rd!"
	else
		script.Parent.Text = "You are " .. rank .. "th!"
	end
	wait(2)
	script.Parent.Visible = false
end)

I hope this is what you are looking for.

4 comments

0

You don't really need the Remote Function codes, but I did it for safety

0

Wow thank you, this is extremely helpful. I haven't really done anything with a Remote function before so this is extremely helpful. I think I did something wrong though. The block only triggers the GUI once. Just to clarify, I should have 1 script under SSS and 2 local scripts under my GUI, right?

0

Yeah

0

Oops! I made a small mistake add debounce = false inside server script service after line 13. I'll edit the post

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