Touched:Connect triggering on play?

I have this code set so that it will make a local script in StarterPlayScripts Disabled = false
However when I play the game the script runs for some reason when I press play?
The local script is already disabled by the way.
Code:

wait()
script.Parent.Touched:Connect(function()
	game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
	print("???")
end)

So as soon as I start up the game this code just runs without me even touching the part?

https://gyazo.com/b17ece3b0bc62c955cbbddd8968d8eef.gif

You need to check if the touched function is triggered by a player or not

Like that:

script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent) then
game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)

If you do it that way, the disable command will work only when a player touches the brick.

script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent) then <---- error right here: Expected ')' (to close '(' at column 3), got 'then'
game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)

Whoops, I forgot a ) at the end, here’s the code:

script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent)) then
game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)

Question where is this script inside, the .Touched one?

Yes, he used script.Parent, so I am guessing that his script is inside the part.

It’s parented to a Part
(30 chars)

What part, in specific?
30 char

Can you elaborate on what you mean?

So, the script in inside a part, in which part is in the script inside?

Did the code I sent you worked? I sent it with a fix, I forgot to add the ) in the if, does it work now?

Yes it prints the command however it does not run the local script, even though in the workspace when it has been touched. The local script becomes enabled but does not run?

Its inside the only part in the workspace that is called “Part”

That’s because you are trying to enable a script which is not inside of the player, to change that inside of the player, do:

script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent)) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.PlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)

You can’t enable/disable a LocalScript with a server script. Furthermore, scripts put into StarterPlayerScripts are cloned into PlayerScripts upon game launch. The former is just a container for them. Changing their properties in it after they’ve been cloned from it won’t have any effect.

1 Like

If you can’t enable it, like Kiriot said, you can use a remote event, to fire the client, and when the remote event fires, the script will run
like:

script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent)) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.RemoteEvent:FireClient(player) --Name the remote event whatever youwant to.
print("???")
end
end)

and in the script that is inside of the player, do:

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
--The code that's in the script that you have tried to enable
end)

Why not just handle the Touched event in the StarterPlayerScripts LocalScript without disabling it?

local plr = game.Players.LocalPlayer
local part = workspace:WaitForChild("SomePart") 
part.Touched:Connect(function(hit)
    local model = hit:FindFirstAncestorOfClass("Model")
    if model and model == plr.Character then
        --part touched by the player, run code here
    end
end) 
1 Like

Do touched events even work inside local scripts?