I was trying to get the script to a screen Gui when the brick is touch but I did not [In roblox] and there does not seem to have any error showing. Why?
Local Script
script.Parent.OnTouched:Connect(function() game.ReplicatedStorage.RemoteEvent1:FireServer() end)
Script
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function() -- Any code in here will run when RemoteEvent is triggered 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.ScreenGuiDirtBlock.Frame.Visible = true wait(0.5) Player.PlayerGui.ScreenGuiDirtBlock.Frame.Visible = false end end) end)
Can anyone tell me why it is not working? And how can I fix it?
Take note: This answer assumes you have experience with remotes, so explanations on remotes wont be very well touched on, same with Filtering Enabled.
There's a few things I noticed when looking at your code.
You're trying to access a player's PlayerGui
via a server script. (Or just a Script
:P) The server's not able to mess with what's in a player's PlayerGui. (A feature of FE)
What's OnTouched
in the client script? (Or LocalScript
)
Why's the client prompting the server so that the server opens the gui? Why not have the server prompt the client instead?
The Solution
Based upon my question in point #3, this would be a much better approach than having the server handle stuff on the client. Here's an example of what I mean:
local ClientRemote = game.ReplicatedStorage.Remote -- Our remote for the server and client communication. local Part = script.Parent -- The script's parent/the part for which a player must touch to fire the below code. local Debounce = true -- A debounce! :D To prevent the `Touched` event from firing *way* too many times. Part.Touched:Connect(function(Hit) -- The `Touched` event to listen for when something touches it, which will then return a parameter: The part that touched it! :O if Debounce and Hit.Parent and Hit.Parent:FindFirstChild('Humanoid') then -- To check if `Debounce` is true, to check if `Hit`'s Parent property isn't nil (b/c bullets), and to check if said parent (if not nil) has a Humanoid. Debounce = false -- Changes `Debounce` to false, thus prevent the code from firing again for a specific amount of time. local Player = game.Players:GetPlayerFromCharacter(Hit.Parent) -- The `GetPlayerFromCharacter` function will return the player if what was given to it was a player's character, if not it'll return nil; in this case, we're checking if what touched the part was a player's character. if Player then -- Checks if it was a player, and if so... ClientRemote:FireClient(Player) -- ...prompt the client. end wait(2) -- Waits a specific amount of time. (In this case, 2 seconds XP) Debounce = true -- Changed Debounce back to true, allowing the code to fire again. end end)
Okay, now we're all finished! :D Now TV, here I come!
Stop right there! What about the client?
Oh yeah...
Now, for the client, we can now set up for it to listen for when it's being prompted.
local ClientRemote = game.ReplicatedStorage.Remote local Gui = script.Parent -- Assuming that the client script (LocalScript) is within the gui function OpenGui() -- Function to fire. Gui.Enabled = true -- Also assuming that the gui isn't enabled. end ClientRemote.OnClientEvent:Connect(OpenGui) -- Listens for when it's prompted.
And now we're done! :D
Stop right there, TheeDeathCaster! Aren't you forgetting something?
Oh, right. Forgot. lol
Stuff touched on, but was never really explained.
GetPlayerFromCharacter
- A function that returns the player if what was given it was a player's character, if not it will return nil.
Debounces
- A way to prevent code from firing multiple times upon execution.
If you have any questions, please don't hesitate to let me know. ^^ I hope this helped! :D
Edit
Being pointed out in the community chat, the original script's changing the visibility of a Frame
and not ScreenGui
. >~< Sorry about that.
To make this edit is fairly simple; you just gotta change up how I set up the client script; for example, changing Gui
from being defined towards the ScreenGui
, it would be changed to be defined as the Frame
, and .Enabled
would be switched out with .Visible
.
Here's edits made to the script to show what I mean:
local ClientRemote = game.ReplicatedStorage.Remote local Gui = script.Parent.Frame -- I added `.Frame` so that the variable `Gui` is defined to the `Frame`. function OpenGui() -- Function to fire. Gui.Visible= true -- I switched out `.Enabled` with `.Visible`. end ClientRemote.OnClientEvent:Connect(OpenGui)
Again, if you have any questions, please don't hesitate to let me know. : )
First, local scripts do not work on Workspace. They should be placed under either StarterPack, or StarterGui. Second, OnTouched does not exist; Touched is correct. Third, you've (mistakenly) made client fire RemoteEvent1, while server checks for RemoteEvent.
So correct code would be (if target is workspace.Part) :
LocalScript
workspace.Part.Touched:Connect(function() game.ReplicatedStorage.RemoteEvent:FireServer() end)
Script
(considering if script is placed under part)
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function() 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.ScreenGuiDirtBlock.Frame.Visible = true wait(0.5) Player.PlayerGui.ScreenGuiDirtBlock.Frame.Visible = false end end) end)
helpful things (not required) : http://robloxdev.com/articles/Debounce