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?
script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent) then
game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)
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)
script.Parent.Touched:Connect(function(hit)
if(game.Players:GetPlayerFromCharacter(hit.Parent)) then
game.StarterPlayer.StarterPlayerScripts["Walk to PointA"].Disabled = false
print("???")
end
end)
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?
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.
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)