Why doesn't this RunContext -> Client script not work?

So, I’ve made a script for a game that’s coming up. I’ve also seen a camera script for a GUI before, but just copied it over to this part. Here’s the script:

local remote = game.ReplicatedStorage.EpicCameraSauce

script.Parent.Touched:Connect(function(plr)
	local camera = workspace.CurrentCamera
	local cameragoal1 = 70
	local cameragoal2 = 70
	local flash = workspace.Sniper.flash
	local sniper = workspace["sniper man"]
	local reload = workspace.Sniper.hold.reload
	local kill = workspace.sniper_KillPart1
	local kill2 = workspace.sniper_KillPart2
	
	kill.Position = Vector3.new(92.392, -15.311, -128.382)
	kill2.camera.Enabled = false

	repeat task.wait()
		camera.CameraType = Enum.CameraType.Scriptable
	until camera.CameraType == Enum.CameraType.Scriptable

	task.wait(0.07)

	if camera.CameraType == Enum.CameraType.Scriptable and camera.FieldOfView == cameragoal2 then
		camera.CFrame = workspace.Menu.SniperCam.CFrame
		camera.FieldOfView = cameragoal1
		print("Worked.")
		wait(0)
		flash.shoot:Play()
		flash.BillboardGui.Frame.ImageLabel.ImageTransparency = 0
		wait(.2)
		sniper.Head.boom:Play()
		sniper.Head.face.Transparency = 1
		sniper.Head.talk.Transparency = 0
		flash.BillboardGui.Frame.ImageLabel.ImageTransparency = .33
		wait(.1)
		flash.BillboardGui.Frame.ImageLabel.ImageTransparency = .67
		wait(.1)
		flash.BillboardGui.Frame.ImageLabel.ImageTransparency = 1
		wait(.225)
		sniper.Head.face.Transparency = 0
		sniper.Head.talk.Transparency = 1
		wait(.15)
		reload:Play()
		wait(.15)
		sniper.Head.face.Transparency = 1
		sniper.Head.talk.Transparency = 0
		wait(.7)
		sniper.Head.face.Transparency = 0
		sniper.Head.talk.Transparency = 1
		wait(2)
		camera.CameraType = "Custom"
		camera.FieldOfView = cameragoal2
		kill.Position = Vector3.new(92.392, 13.382, -128.382)
		kill2.camera.Enabled = true
	elseif sniper.Head.boom.Playing == true then
		while true do end
	elseif camera.CameraType == not Enum.CameraType.Scriptable then
		print("Why is the Camera not Scriptable? Oh yeah, we didn't waited long enough, or the camera won't be scriptable.")
		--Do you want more if the camera's FOV is 120?
	elseif camera.FieldOfView == cameragoal1 then
		camera.FieldOfViev = cameragoal2
		print("FOV: "..camera.FieldOfView)
	end
end)

The problem is that the script doesn’t just doing for client, it does it for everyone in the server. (The RunContext is set to Client, not Legacy.)
What should I do? Should I set the camera script to a LocalScript, and set the parts to a normal script, or something else?

TDLR; The script is fine except is not client sided. What do I do?

Your script is still running on the client. However the way the function works is that when anything touches “script.Parent”, everyone gets affected by it as the Touched event detects the same thing for every client. There should be zero difference to using a local script compared to setting RunContext to Client anyway.

You just need to add a check on if whatever is touching it is a player and if the player that is touching it is the client (aka LocalPlayer).

Just add this to the beginning of your Touched function:

local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
if not Player or Player.Name ~= game.Players.LocalPlayer.Name then
	return
end

or

-- Much simpler than the above, but doesn't prevent the very rare case 
-- of a non-player model the same name as a player from also 
-- running the script.
	if hit.Parent.Name ~= game.Players.LocalPlayer.Name then
		return
	end

Also I don’t entirely know how your script works but I wouldn’t recommend doing while true do end near the bottom of your script, as that is an infinite loop that may freeze up the client. I recommend just replacing it with return.

Should I replace “script.Parent.Touched:Connect(function(plr)” with (function(hit) instead? Also, I already had plr, so would we need that local… thingy?

1 Like

Oh sorry, I didn’t catch that. Yes, you should replace plr with hit in your function. “plr” isn’t actually the player, just the part that touched it, so you should keep the Player variable if you’re using the first code.

1 Like

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