Code Running Once then Stopping

You can write your topic however you want, but you need to answer these questions:

Hello, I am making a camera system, but for now I made it so when you activate a proximity prompt
it opens the gui.

Whenever the player activates the prompt it opens up the gui, then everything is good. But once they close the gui and click the prompt again it doesn’t work. It seems the prompt is only working once.

local prompt = script.Parent.ProximityPrompt
local debounce = true

prompt.Triggered:Connect(function(player)
	print("Worrrrk") --This runs every time the player clicks it

	game.Workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = 0  --This part of the code only works one time?
	game.Players:FindFirstChild(player.Name).PlayerGui.Cameras.Enabled = true --This part of the code only works one time?

end)

I’ve looked everywhere for this but I do not know how to fix this. If anybody could help then thanks.

When you set the humanoidwalkspeed to 0 and the camera to true, the values likely stay the same. Could you put a print at the end and see if the function isn’t terminating?

Here is the new code for that, it did get to the end.

local prompt = script.Parent.ProximityPrompt
local debounce = true

prompt.Triggered:Connect(function(player)
	print("Worrrrk")

	game.Workspace:FindFirstChild(player.Name).Humanoid.WalkSpeed = 0 --Doesnt so this
	game.Players:FindFirstChild(player.Name).PlayerGui.Cameras.Enabled = true --Doesnt so this
	
	print("Got to the end!")
end)

Here is the code for the script that closes the GUI:

local nextButton = script.Parent.Next
local backButton = script.Parent.Back
local current = script.Parent.Current
local cameras = script.Parent.Camera:GetChildren()

local currentCam = 1



nextButton.MouseButton1Click:Connect(function()
	if currentCam < 4 then
 	currentCam += 1
	current.Text = "Camera "..currentCam
		
		for i, v in pairs(cameras) do
			if v:FindFirstChild("number").Value == currentCam then
				print("true")
				v.Visible = true
			else
				print(currentCam)
				print("didnt get it")
				v.Visible = false
			end
		end
		
		end
end)

backButton.MouseButton1Click:Connect(function()
	if currentCam > 1 then
	currentCam -= 1
	current.Text = "Camera "..currentCam
		for i, v in pairs(cameras) do
			if v:FindFirstChild("number").Value == currentCam then
				print("true")
				v.Visible = true
			else
				print(currentCam)
				print("didnt get it")
				v.Visible = false
			end
		end
		end
end)

script.Parent.Exit.MouseButton1Click:Connect(function()
	script.Parent.Enabled = false
	script.Parent.Camera["1"].Visible = true
	script.Parent.Camera["2"].Visible = false
	script.Parent.Camera["3"].Visible = false
	script.Parent.Camera["4"].Visible = false
	currentCam = 1
	script.Parent.Current.Text = "Camera "..currentCam
	game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).Humanoid.WalkSpeed = 13

end)

Could you try simply defining the player character as a variable? I noticed you’re doing game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).Humanoid.WalkSpeed = 13

Instead, could you try doing
local character = game.player.character or game.player.CharacterAdded:Wait()

I’m wondering if it’s simply struggling to find the character model. I don’t imagine it is but I’d just like to make sure.
If possible, can you ensure that the values still ‘exist’ and print out their states before and after?

like this?

local char = game.Workspace:FindFirstChild(player.Name)
	char.Humanoid.WalkSpeed = 0 --Doesnt so this

You can get the character model from a player instance by just doing player.Character. As it is on the client, it’s generally good habit to check to ensure that the character exists (hence why I posted it above).

	print("Worrrrk")
local char = game.Players[player.Name].Character
	char.Humanoid.WalkSpeed = 0 --Doesnt so this
	
	game.Players[player.Name].PlayerGui.Cameras.Enabled = true --Doesnt so this
	
	print("Got to the end!")

this still doesnt work

Is this a server script or local script?

local prompt = script.Parent.ProximityPrompt
local debounce = true

prompt.Triggered:Connect(function(player)
	if debounce == true then
		debounce = false
		print("Worrrrk")
		local char = game.Players[player.Name].Character
		char.Humanoid.WalkSpeed = 0
		if char.Humanoid.WalkSpeed == 0 then
			print("yayChar")
		else
			print("no char")
		end
		game.Players[player.Name].PlayerGui.Cameras.Enabled = true
		if game.Players[player.Name].PlayerGui.Cameras.Enabled == true then
			print('yay')
		else
			print('no')
			end

		print("Got to the end!")
		debounce = true
	end
	
end)	

This is printing always “Yay” and “yayChar” and “Got to the end” it never prints no. It works the first time, but once i close the gui with a guibutton and try the prompt again. It prints these out but I dont see anything and my walkspeed doesnt change.

It’s a script inside a part, the part also holds the prompt.

Is the script a serverscript(just called script) or a localscript?

to fix this just create a remote event that enables the camreas.

  1. Make a RemoteEvent in ReplicatedStorage and name it something like EnableCameras

  2. Make a local script under the Cameras (the gui that you are trying to enable) then copy and paste this code in it

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()

game.ReplicatedStorage.EnableCameras.OnClientEvent:Connect(function() -- Change the 'EnableCameras' to whatever you named the RemoteEvent
	script.Parent.Enabled = true
	Character:WaitForChild('Humanoid').WalkSpeed = 0
end)

  1. Change the prompt code to
local prompt = script.Parent.ProximityPrompt
local debounce = true

prompt.Triggered:Connect(function(player)
	print("Worrrrk") --This runs every time the player clicks it

	game.ReplicatedStorage.EnableCameras:FireClient(player)

end)

lol sorry I thought you meant serverscriptservice.
yeah its a serverscript

you’re the best thanks for helping. I was going to try a remote event later but I thought it might be a little too much to enable the cameras. Thanks a lot!

Thank you to for helping the most.

lol sorry i shouldnt be posting here anymore bye now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.