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

How to make GUI which pops up on touch, filtering enabled?

I am trying to make it so when the player touches this brick, a GUI is cloned and pops-up on the clients screen, and with that he can purchase a car and it will spawn in a designated area. When I try it in studio, it works fine, but in game it does not respond. The GUI (called SpawnGUI) will open, however none of the scrolling buttons nor the buy button works.

I have posted a copy of the script below. I am not used to filtering enabled, so I am not sure how to use remote events or remove functions. The player must click the buy button, which takes away 50 "Money" from the leaderstats and places the vehicle desired (all vehicles located in ServerStorage) in a specific place where all the cars will spawn.

Script for spawning GUI

script.Parent.Touched:connect(function(p)
	local player = game.Players:GetPlayerFromCharacter(p.Parent)
	if (player) then
		if (not player.PlayerGui:FindFirstChild("SpawnGUI")) then
			local GUI = game:GetService("ServerStorage").SpawnGUI:Clone()
			GUI.Regen.Value = script.Parent.Parent.Pos
			GUI.Parent = player.PlayerGui
		end
	end
end)

local s = 0
while wait(0.1) do
	s = (s + 2) % 360
	script.Parent.UI.StudsOffset = Vector3.new(0,5 + math.sin(math.rad(s)),0)
end

Script for giving car:

local P = script.Parent
local player = P.Parent.Parent
local index = 1

local vehicles = {

	{"Toyota Corolla 2010"	, 0}, 
	{"Smart Fortwo"	, 0},
	{"Sudiko Green Yoga"	, 0},
	{"Sudiko Blue Yoga"	, 0},
	{"Toyota Prius 2010"	, 0},
	{"BMW i3"	, 0},
	{"TCI"	, 0},
	{"Toyota Prius 2016"		, 0}
}

wait(0.5)
-- GUI vars
local bg = P.BG

-- Helper function
function SetInformation(index)
	bg.I.Image = "http://www.roblox.com/thumbs/asset.ashx?assetId="..vehicles[index][2].."&x=420&y=420"
	bg.T.Text = vehicles[index][1]
end

local locked = false
bg.S.MouseButton1Click:connect(function()
	local Money = player.leaderstats:FindFirstChild("Money")
	if Money.Value > 50 then
		Money.Value = Money.Value - 50
		
	if (locked) then return end
	locked = true
	local vehicleName = player.Name.."Car"
	local model = workspace:FindFirstChild(vehicleName)
	if (model) then model:Destroy() end
	model = game.ServerStorage.Vehicles:FindFirstChild(vehicles[index][1]):Clone()
	model.Name = vehicleName
	model.Parent = game.Workspace
	model:MakeJoints()
	model:MoveTo(P.Regen.Value.Position)
	P:Destroy()
	locked = false	
		
	else
		script.Parent.BG.S.Text = "Not Enough Money, 50 Needed!"
		wait(3)
		script.Parent.BG.S.Text = "Spawn"
	end
end)

bg.B.MouseButton1Click:connect(function()
	index = index - 1
	if (index < 1) then index = #vehicles end
	SetInformation(index)
end)

bg.N.MouseButton1Click:connect(function()
	index = index + 1
	if (index > #vehicles) then index = 1 end
	SetInformation(index)
end)

bg.Q.MouseButton1Click:connect(function()
	P:Destroy()
end)

SetInformation(index)

Please help me find a solution that it will work with filtering enabled, and please describe in detail the steps needed. Thankyou!

7 comments

0

well, does the Dev Console tell anything whilst playing the game?

0

Nope. All it says is the meshes failed to load

0

Make the second script a LocalScript

0

The second script being a localscript would be bad. That would make it so that the car only shows up on the player's system, so other players would not notice it.

0

Then what should I do?

0

Just dump the Touched function in the client. much easier. no server communication needed.

0

And how would I do that? Remember im a beginner

1 answer

✓ Accepted answer

Make sure you read/understand this: https://scriptinghelpers.org/guides/server-client-relationship-in-roblox

The problem is that your car-giver script is trying to do both server-related activities (generating a car, which you want everyone to be able to see/interact with) and client-related activities (GUI, which you only want to show up for one player). The GUI events aren't even replicated from the client to the server, which is why those MouseButton events aren't firing. You need to move all that GUI code into a LocalScript and then let the server know when the player has requested the purchase of a vehicle using either a RemoteEvent or a RemoteFunction.

Most of your code will either be in one script or another, but when it comes to making sure the player has enough money, you ideally check it in both. The server should check it because you should never trust the client (an exploiter can activate RemoteEvents/Functions with any arguments they want), and you the client should check it to prevent unnecessary communication with the server and lag time (why ask the server to check if the player can afford it, which may take half second, when they could be notified immediately if the client checks first?)

[Edit - Response to comments]

Either Remote Function or event? Is one more preferable?

You can read up on the differences in the API for RemoteEvents and RemoteFunctions, but the essentially you use an Event when you want 1-way communication (ex server telling client that they've been given something) and a Function when you want 2-way (ex client asking server to purchase a vehicle - the server could then respond with whether the purchase was successful)

Like what script do I put in the Remote event/function and how do I fire it

You can put them anywhere you want (except in client-only and server-only areas, like ServerStorage, in a player's GUI, etc), just keep it organized. Putting them in ReplicatedStorage, possibly in a folder, or as a child of the script that handles them are perfectly reasonable. To fire it, you should look at the links above - the API tells you what functions are available. Also, for examples, they have this tutorial: http://wiki.roblox.com/index.php?title=Remote_Events_and_Functions

11 comments

0

Either Remote Function or event? Is one more preferable? Also, I have transferred all the data to a local script. How do I "fire" the remote event/function? Insert a script or something?

0

Like what script do I put in the Remote event/function and how do I fire it

0

Ok so had read the guide you posted and understand that now the client cannot make changes to the server. I get it, but how do I fire the remove event/function? Please?

0

I have responded in the answer

0

Ok so your line on remote events and functions doesnt work. I have looked at the wiki but Im not trying to create a part. So basicly, I should create a remote function in the workspace and add a script to it. But what do I put in the script? I dont get it. Also, what do I put in the GUI script so it knows to fire the remote function?

0

Also, if you want to try it out or something the model is "Car Spawner" by dshorter100

0

"your line on remote events and functions doesnt work" - what? "I have looked at the wiki but Im not trying to create a part." - replace the part where it makes a part with whatever you want it to do. The wiki gives you an example to learn from so you can apply it to your situation. (And, sorry, not up to looking at that model at this time.)

0

Thats the thing I do not know what to put. Im really new. Could you clarify what to put in the Remotefunction & the GUI script so the remote event can be fire for a GUI?

0

Please, this would be a HUGE help me to me :) This is the last major thing I am stumped on. Please help me :)

0

Please instruct me on what scripts to put. I know I am repeating myself but you dont not understand how helpful this would be to me bro. :)) Like the first question I posted

0

Please?

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