Help with touched script

I made this script for my gasa4 game that when a player touches a part it makes a ui apear but its not working and theres not errors

its a local script in startergui

local part1 = game.Workspace.CarNPC.Car.Car.CarBody
local part2 = game.Workspace.CarNPC.Car.Car.CarBody
local part3 = game.Workspace.CarNPC.Car.Car.CarBody
local EndingUI = game.StarterGui.EndingUI


part1.Touched:Connect(function()
	wait(0.1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"


part2.Touched:Connect(function()
	wait(0.1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"


part3.Touched:Connect(function()
	wait(0.1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
		end)
	end)
end)

also if I change the line 4 variable to script.parent it just makes the ui apear without touching the part would I have to add an
else EndingUI.Enabled = false?

1 Like

bumpppp???

1 Like

You are declaring the Template of the GUI you want to change its Enabled property, Not the GUI that is visible and controllable to the player itself.

You would declare the EndingUI as it was in the PlayerGui container.
Here is an example:

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui
local EndingUI = PlayerGui.EndingUI
1 Like

im not sure what you mean by template?

All the things that are in the StarterGui container are cloned to the player’s PlayerGui container (there are some exceptions).

You can learn more on this documentation page.

ah i get it now ok

so is that all i would have to do?
i dont know how to code at all lol making this script was already a challenge
but i tested it and the ui still randomly apeared

local part1 = game.Workspace.CarNPC.Car.Car.CarBody
local part2 = game.Workspace.CarNPC.Car.Car.CarBody
local part3 = game.Workspace.CarNPC.Car.Car.CarBody
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui
local EndingUI = PlayerGui.EndingUI


part1.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"


part2.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"


part3.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
		end)
	end)
end)

Yes, the GUI ‘EndingUI’ is correctly declared.


Recheck your connections and other stuff that makes it enabled.
Just noting, you’re not making it invisible again…

1 Like

yes I know im not making it invisible again its gonna stay on the players screen but i only have 1 script to activaet the ui and thats it im also not touching the part and its still making the ui apear not sure why

could you use like an else EndingUI.enabled = false so that it stays false until you touch the car

Check if it is enabled by default in the StarterGui.

its not it doesnt apear as soon as i start the game but it randomly apears

There might be another script that is enabling it?!

If the parts are correctly positioned and sized, I don’t see a problem in the script you provided actually.

ya this is really annoying ive been spending like 3 hours trying to fix this and I gotta realese my game today

I dissabled the script to make the ui apear and it stopped apearing so its gotta be the script

Check the connections, then.

Just wait a moment…
You’re connecting the touched events in a wrong way (the ends).

Here is the modified script:

local part1 = game.Workspace.CarNPC.Car.Car.CarBody
local part2 = game.Workspace.CarNPC.Car.Car.CarBody
local part3 = game.Workspace.CarNPC.Car.Car.CarBody
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui
local EndingUI = PlayerGui.EndingUI


part1.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
end)

part2.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
end)

part3.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
end)

Side note: use task.wait() instead of the deprecated wait()

1 Like

oh lemme try this really hope it works

still doesnt work the weird thing is it doesnt apear when i do touch the car but it will randomly apear

Try to check the position and size properties of the declared parts.
Also, why are you connecting the touched event three times on a single part?

1 Like

its actully multiple parts just with the same name im changing the name tho since that might be the problem

I decided to try and but the script under the part that triggers the ending

local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player.PlayerGui
local EndingUI = PlayerGui.EndingUI

script.Parent.Touched:Connect(function()
	wait(1)
	EndingUI.Enabled = true
	EndingUI.TextLabel.Text = "HitByCar"
	EndingUI.TextLabel1.Text = "Ending"
end)

and i got the error
attempted to index nil with player.playgergui

the good news is it seems that its not randomly appearing anymore

maybe i would have to do player.localplayer.playergui?

It looks like part1 == part2 == part3