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)
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.
Make sure your script is not a local script.
You should use RemoteEvents to communicate between server-client. So create a Remote Event into ReplicatedStorage.
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)
Go to StarterGui and insert a local script.
Type this to beginning of the Local Script:
game:GetService("ReplicatedStorage").RemoteEvent.OnClientEvent:Connect(function()
--// Disable the UI script here.
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)
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
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