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!
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