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

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

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

✓ Accepted answer

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!

2 comments

0

Thank you! I put the scripts in game.StarterGui and now it works perfectly!

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)

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.

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