Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

FireServer is not working on Touch Event?

Asked by 3 years ago

Hello, I made a Touch Event that whenever the player's character touches the part, it will fire a RemoteEvent. Easy right? Wrong. I put the FireServer script in a local script because Firing the server only works on clients.

FireServer Local Script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        game.ReplicatedStorage.ShowCodeGUI:FireServer()
    end
end)

OnServer Script:

game.ReplicatedStorage.ShowCodeGUI.OnServerEvent:Connect(function(player)
    local playerGUI = player:WaitForChild("PlayerGui")
    local CodeFrame = playerGUI:FindFirstChild("CodeGUI"):FindFirstChild("CodeFrame")
    CodeFrame.Visible = true
end)

Also CodeGUI is located in StarterGUI, meaning it will get cloned and will be located in Player's PlayerGUI.

3 answers

Log in to vote
0
Answered by 3 years ago

You can't insert a LocalScript inside a Part, instead please move your LocalScript to StarterGui And please replace

script.Parent.Touched

To:

game.Workspace.PartName.Touched

This must work though.

0
omg, tysm!!! :D rayhansatria 142 — 3y
0
no problem Gabe_elvin1226aclan 323 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

(read the code as tl;dr srry i put max effort on everything)

According to the Wiki page on LocalScript, your FireServer script will not run since it is parented to a part which, I assume, is not a descendant of the player's client-side services.

Solution 1

Perhaps you could flip things around - FireServer will be ran on the server, whilst the player's client will be handling the event. By then, you can spend a minute on readjusting how the scripts will have to function for them to properly handle the event, such as switching from :FireServer() to :FireClient(plr), etc.

--[[
   FireServer but it's a normal Server Script
   Its Parent is still the same doe
]]
local players = game.Players

script.Parent.Touched:Connect(
    function(hit)
        --You definitely need to run a Player check here instead of checking for Humanoid
        local parent = hit.Parent
        local plr = players:GetPlayerFromCharacter(parent)
        if plr then --If player exists then
            --Fire the event to the target's client.
            game.ReplicatedStorage.ShowCodeGUI:FireClient(plr)
        end
    end
)

--[[
   OnServer but it's a LocalScript
   Its Parent is the CodeGUI itself to make things ez
]]
local player = game.Players.LocalPlayer

game.ReplicatedStorage.ShowCodeGUI.OnClientEvent:Connect(function()
    --I leave these here in case this script's Parent isn't CodeGui
    --local playerGUI = player:WaitForChild("PlayerGui")
    --local CodeFrame = playerGUI:FindFirstChild("CodeGUI"):FindFirstChild("CodeFrame")

    local CodeFrame = script.Parent:FindFirstChild('CodeFrame')
    CodeFrame.Visible = true
end)

Solution 2

But if for some reasons, you wanna keep FireServer... local, then you can change the parent of the FireServer script to somewhere else the script can run in, as the Roblox Wiki has enlisted. Once it is changed, create a variable as a reference to the part in you needs, and change things up a little bit:

--[[ 
  FireServer LocalScript (Put this in anywhere the script runs such as StarterPlayerScripts)
]]
--Add a reference variable to the part you wanna check.
local partToCheck = workspace.ExamplePart --Your part

partToCheck.Touched:Connect(
    function(hit)
        --Maybe you wanna check for player instead? Check the one above, then I'll leave that to ya
        if hit.Parent:FindFirstChild("Humanoid") then
            game.ReplicatedStorage.ShowCodeGUI:FireServer()
        end
    end
)

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Do NOT waste remotes on touch events.

I added debounce and some extra protection so it doesn't overload the character with guis, and theres now a cooldown. feel free to set the "cooldown" variable to 0 if you dont want a cooldown at all

Here it is; editing is required to make it work.

local function insert(player)
    if player.PlayerGui:FindFirstChild('gui name') then return end
    -- add gui insert code!!
end

local cooldown = 2
local cooldown_ = false

local part = script.Parent
part.Touched:Connect(function(hit)

    if cooldown_ then return end -- debounce

    if game.Players:FindFirstChild(hit.Parent.Name) and hit.Parent:FindFirstChild('Humanoid') then
        cooldown_ = true
        insert(game.Players:FindFirstChild(hit.Parent.Name))
        wait(cooldown)
        cooldown_ = false
    end
end)

It's MUCH more convenient, and Touched events can be used on Server & Client.

(Not trying to be rude and all, but) please do a bit more research before you post a silly question. Remotes should only be used when required, like for sending a lot of data to the server, etc.

I hope this helps you!

  • infinite / gam3r
0
Also make sure to remove the client script as its not necessary for my script! GAM3RBOY2008 0 — 3y

Answer this question