Need help with triggering GUI with touched event

Hi, Scripters! I have a task I need help with where I’ve tried so hard to make it function. In this local script, Badpart is the part that is being touched, IntVal is a value being stored in ReplicatedStorage and Luck is a Text Lable being stored in the StarterGui, held inside a ScreenGui. When the block is touched, the numbers one or two will be generated from another script that leads to the IntVal in ReplicatedStorage. Then, this script finds that value and uses it to judge which if statement it fits into.

As you can see, I’ve added some print statements for testing purposes, which do print out. The only issue I’m having is actually making the text appear on the player’s screen. Here is the code:

local Badpart = game.Workspace.Badpart
local IntVal = game.ReplicatedStorage.IntVal
local Luck = script.Parent.Luck

Luck.Text = "Test"
Luck.Text = ""

while true do
	if Badpart.Touched == true then
		if IntVal.Value == 2 then
			print("You doubled your Money!")
			Luck.Text = "You doubled your Money!"
			Luck.Text = ""
		elseif IntVal.Value == 1 then
			print("Oh no! You lost half your money!")
			Luck.Text = "Oh no! You lost half your money!"
			Luck.Text = ""
		print("GUI Working...")

I will be active for a decent bit if you have any more questions about the code. Thanks to those who can help!

1 Like

Assuming the part touch script is a server script parented to a part which itself belongs to the workspace, as such because the StarterGui for all players is client-sided, you’ll need to fire a client event from the server script when the part is touched, with this client event then being handled by a local script which performs the desired alteration to the particular clients (player which touched part) StarterGui.

you can simply do it by getting the player who touched the part and then toggling their gui, something like this

local Players = game:GetService("Players")
local part = -- your part

part.Touched:Connect(function(hit) -- hit is who touched it
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then -- checks if it's a player character who touched it
        local plr = Player:GetPlayerFromCharacter(hit.Parent)
        local gui = plr.PlayerGui.--your gui
        -- put your gui trigger function here