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

Touch:Connect(function(hit) only works once?

Asked by 4 years ago

So I was making a Simulator (you may already know on my first question) I wanted to add a touch event to a MeshPart to open up the Frame. It works but only once for some reason.

The shop script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if plr then
            plr.PlayerGui.Shop.Frame.Visible = true
        end
    end
end)

The rebirth script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if plr then
            plr.PlayerGui.Rebirth.Frame.Visible = true
        end
    end
end)

NOTE: BOTH OF THESE ARE SERVER SCRIPTS

Any help would be appreciated!

2 answers

Log in to vote
0
Answered by 4 years ago

Try putting them both in LocalScripts. GUIs should usually be accessed by clients.

Shop Script

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
            game.Players.LocalPlayer.PlayerGui.Shop.Frame.Visible = true
        end
    end
end)

Do the same with your rebirth script by using game.Players.LocalPlayer instead of GetPlayerFromCharacter.

Hope this helps!

0
Thank you! I put the scripts in game.StarterGui and now it works perfectly! Filip100012 58 — 4y
0
The code: workspace.ShopMesh.Touched:Connect(function(hit) print(hit.Parent) if hit.Parent:FindFirstChild("Humanoid") then script.Parent.Shop.Frame.Visible = true end end) Filip100012 58 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You cannot modify GUI's on the server, you need to convert the scripts script into localscripts and just use LocalPlayer to check if they touched the part.

Answer this question