Script restarts every time the player dies

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

  1. What do you want to achieve? Keep it simple and clear!

i wanna fix this issue so that the camera wont move to that position every time i die

  1. What is the issue? Include screenshots / videos if possible!

when dying the camera moves to the position i put it when making the menu
https://gyazo.com/692082277af277a70004b8f132cb9549

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

i tried deleting, destroying, disabling the script but its still running

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this is the script that sets the position of the camera to for the main menu

local cam = workspace.CurrentCamera

local Players = game:GetService("Players")
local player = Players.LocalPlayer

cam.CameraType = "Scriptable"

local pos = Vector3.new(-200, 50, -50)
local lookAt = Vector3.new(0,0,0)
local cameraCFrame = CFrame.new(pos, lookAt)
workspace.CurrentCamera.CFrame = cameraCFrame

player.CameraMinZoomDistance = 5
player.CameraMaxZoomDistance = 10

and heres the script that moves the camera back to the character

local button = script.Parent

local function onButtonActivated()
	button.Parent.Parent.Visible = false
	
	game.Workspace.CurrentCamera.CameraType = "Custom"
	game.StarterPlayer.StarterCharacterScripts.cameraLocalScript:Destroy()
	
	game.Lighting.menuDepth.Enabled = false
end

button.Activated:Connect(onButtonActivated)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

Put the script in StarterPlayerScripts instead of StarterCharacterScripts.

2 Likes

“StarterPlayerScripts” is where you put scripts that affect the entire player’s game experience, player management, or global game mechanics.

“StarterPlayerCharacter,” on the other hand, is where you customize the initial appearance and properties of the player’s in-game character, including their model, animations, and initial state.

It’s about controlling the player’s avatar, while “StarterPlayerScripts” handles overall game behavior and interactions."

The code located in StarterPlayerCharacter is executed at the same time as the “CharacterAdded” event since the content of StarterPlayerCharacter is cloned in the character of the player.

This should explain why the code is executed every time.

1 Like

About the second code which is probably located in the ScreenGui, this property of the ScreenGui should interest you:

ResetOnSpawn | Documentation - Roblox Creator Hub

1 Like

i’ve moving the script into starterPlayerScript but it breaks the script, it doesnt give any error message but it just doesnt work anymore

You put button.activated instead of button.mousebutton1click, that may explain the issue,
looking at the documentation though, I may be wrong.

1 Like

You could put a value that is locally switched upon the player first joining the game. Assuming the script is a LocalScript and you only want it to run when the player first joins.

if workspace:FindFirstChild("ScriptAlreadyRun")== nil then
local val = Instance.new("BoolValue")
val.Parent = workspace
val.Name = "ScriptAlreadyRun"
end

if workspace:FindFirstChild("ScriptAlreadyRun").Value ~= true then
workspace:FindFirstChild("ScriptAlreadyRun").Value = true

--your code here--

end

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