Disabling a script on touch

Basically I have an onTouch function in a part, the function is supposed to disable a script in the localplayer’s startergui, but it doesn’t work.

local player = game.Players.LocalPlayer

function onTouch(part) 
	player:FindFirstChild("CountdownGui").TextButton.Script.Disabled = true
end 

script.Parent.Touched:connect(onTouch)

Is there a way to fix it?

2 Likes

player.PlayerGui:FindFirstChild

1 Like

I did try that already, but it didn’t work.

1 Like

I will attempt to see in studio then.

1 Like

Hello,

You should make the touch script as a normal script instead of Local Script.
You won’t able to access to player’s UI if you are using server-sided script.

Now I’ll help you with that.

  1. Make sure your script is not a local script.

  2. You should use RemoteEvents to communicate between server-client. So create a Remote Event into ReplicatedStorage.

  3. Change your script to this:



function onTouch(part) 
	if part.Parent.ClassName == "Model" then
			for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == part.Parent then
			game:GetService("ReplicatedStorage").RemoteEvent:FireClient(player)
		end
		end
	else
			for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == part.Parent.Parent then
			game:GetService("ReplicatedStorage").RemoteEvent:FireClient(player)
		end
		end
	endend 

script.Parent.Touched:connect(onTouch)
  1. Go to StarterGui and insert a local script.

  2. Type this to beginning of the Local Script:

game:GetService("ReplicatedStorage").RemoteEvent.OnClientEvent:Connect(function()
--// Disable the UI script here.

Hope this helps.

Topics:

1 Like

That’s totally wrong, Touched event must be a Script but changing the user’s UI or something requires a local script.

1 Like

Step 5 displays an error, anything else I can try?

1 Like

Correct me if I’m wrong, but didnt the script still not locate the player’s gui itself?

1 Like

I re-edited script. Try again.

1 Like

I didn’t understand that, but this script is in a part. When someone touch to part, this script will try to access to player’s UI but it will fail.

1 Like

it is possible to change the gui’s instances or any parts of it from a localscript parented in a part, localscript can only access player mechanics such as the mouse, by the way make sure the player exists just in case

dont forget that parts that aren’t player may touch this part and trigger this function which the part doesn’t have acquired requirements to and yield a error about a nil object

also i suggest doing this on a server script or regular script variant since that both works if you want for some server reasons

local object = script.Parent

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid == nil and humanoid.Health <= 0 then return end
    local player = game.Players:GetPlayerFromCharacter(part.Parent)
    if player == nil then return end
    player.PlayerGui:FindFirstChild("CountdownGui").TextButton.Script.Disabled = true
end 

script.Parent.Touched:connect(onTouch)
3 Likes
local Part = script.Parent

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player == nil then return end
		Player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Script.Disabled = true
	end
end

Hope this helps.

1 Like

Correct me if I’m wrong, but I encountered an error when I ran your script. Wouldn’t it be preferable to write it like this?
local Part = script.Parent

Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if Player == nil then return end
		Player.PlayerGui:FindFirstChild("ScreenGui").TextLabel.Script.Disabled = true
	end
end

either way works, you probably set something wrong sorry for the late response lol

Oh no worries lol! Thanks for letting me know!