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

[PRO Needed] Can anyone check if my script is correct? My script does not work right.

I have written some script that I test in Roblox Studio, and it works, but when I tested on Roblox(the real game) It does not work?

Here are the scripts

TextButtonGui Script

script.Parent.MouseButton1Click:connect(function()
	script.Parent.Parent.Frame.Visible = true
end)

Brick Script

local Part = script.Parent

Part.Touched:connect(function(HIT)
	local H = HIT.Parent:FindFirstChild("Humanoid")
	if H then
		local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
		Player.PlayerGui.ScreenGuiDirt.Frame.Visible = true
	end
end)

Please help!

3 comments

0

Player gui is invisible to the server, thus you shouldn't use a server script to change it (also explains why it works on studio since it is client only bar local server)

0

also use :Connect :connect is deprecated

0

Can you post a answer tho?

1 answer

✓ Accepted answer

It seems you have not understood the principles of Remote Events and its usage in Roblox Games

What is Filtering Enabled?

Filtering Enabled is a property of Workspace that determines if changes from the Client should be replicated to the Server or not. Think of it as a Boundary that separates two different country.

Now, to get into the other country, you will need a Passport. And that Passport is what we call Remote Events and Functions

What are Remote Events and Remote Functions?

Remote Events and Functions are Roblox Instances that allows communication between Server Script and Client Script. By communication, it means that it allows one script to listen to the other scripts instructions.

##Procedure

  • Make sure FilteringEnabled is checked on, It can be found by clicking on Workspace and going over its Properties in Properties menu.
  • Start by hovering over Replicated Storage and insert a Remote Event.
  • Name the Remote Event to your desire.
  • Insert a Local Script in StarterPlayerScripts and a Server Script in Workspace

Server Sided Script

  • Begin by declaring variables that you might possible use.
  • Create a new function that detects when a Part is Touched.
  • Connecting the function to Touched Event.
-- [Declaration Section]
--\\ Game Services 
local ReplicatedStorage                       = game:GetService("ReplicatedStorage");
local RemoteEvent 							  = ReplicatedStorage:WaitForChild("RemoteEvent");

--\\ Part Location
local Part 								      = workspace.Part;

-- [Processing Section]
local function On_Part_Touched (x)
	print("What touched me?" .. x.Name);
	
	if x.Parent:FindFirstChild("Humanoid") then 
		local Player = game:GetService("Players"):GetPlayerFromCharacter(x.Parent);
		RemoteEvent:FireClient(Player);
	end;
end;

-- [Connecting Section]
Part.Touched:Connect(On_Part_Touched);

Client Sided Script

  • Begin by declaring the same variables you declared before.
  • Create a new function that detects when Remote Event is fired.
  • Connect the event using OnClientEvent
-- [Declaration Section]
--\\ Game Services 
local ReplicatedStorage                       = game:GetService("ReplicatedStorage");
local RemoteEvent 							  = ReplicatedStorage:WaitForChild("RemoteEvent");

-- [Processing Section]
local function On_Client_Event ()
	print("I was Invoked");
end;

-- [Connecting Section]
RemoteEvent.OnClientEvent:Connect(On_Client_Event);

Description

So what exactly did we do in both scripts. Lets begin with Server Script.

Server Script:

  • Created a function that fires RemoteEvent using FireClient function.
  • Will only FireClient when Humanoid is found. Thus will not fire when Part is not touched by Humanoid Part.
  • FireClient() requires Player argument because we need to have an identity. A Passport without an identity cannot be a passport.

Now lets see on Local Script

Local Script:

  • Created a function that is invoked when RemoteEvent has been fired.
  • If it has been fired, it will print I was invoked,
  • if it has not been fired, then it won't print anything.

Now let's apply this concept on your actual script.

Server Sided Script.

-- [Declaration Section]
--\\ Game Services 
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent");

--\\ Part Location
local Part = workspace.Part;

-- [Processing Section]
local function On_Part_Touched (x)
	print("What touched me?" .. x.Name);
	
	if x.Parent:FindFirstChild("Humanoid") then 
		local Player = game:GetService("Players"):GetPlayerFromCharacter(x.Parent);
		RemoteEvent:FireClient(Player);
	end;
end;

-- [Connecting Section]
Part.Touched:Connect(On_Part_Touched);

Client Sided Script.

-- [Declaration Section]
--\\ Game Services 
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui");

local RemoteEvent  = ReplicatedStorage:WaitForChild("RemoteEvent");

--\\ Gui Location 
local ScreenGui = PlayerGui:WaitForChild("ui");
local Button = ScreenGui.TextButton;
local Frame  = ScreenGui.Frame;

-- [Processing Section]
local function On_Client_Event ()
	print("Invoked");
	Frame.Visible = true;
end;

local function On_Button_Clicked ()
	script.Parent.Parent.Frame.Visible = true; --Consider declaring variables for each `Parent` instead. 
end;

-- [Connecting Section]
RemoteEvent.OnClientEvent:Connect(On_Client_Event);
Button.MouseButton1Down:Connect(On_Button_Clicked);
Have a lovely day of coding.

2 comments

0

thx

0

Good luck.

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