A moving flashlight!

Glad to see someone who actually thinks my tutorial is useful. Thank you.

1 Like

THANK YOU BRO! You don’t know how many tutorials I went through, just to try and make a stupid brick arm follow my camera. Your tutorial taught me what I needed, and now everything works. Very appreciative of your tutorial because I am still learning how to script and am teaching myself. :grinning: :grinning:

2 Likes

Good resource, one thing though.

If this does happen, you should add a return, because it currently errors since it can’t find the character anymore on the next lines.

Code:

script.Parent.Activated:Connect(function()
	script.Parent.Handle.Light.Enabled = not script.Parent.Handle.Light.Enabled
	script.Parent.Handle.Sound:Play()
end)

script.Parent.Event.OnServerEvent:Connect(function(player,axis)
	if axis > 1 or axis < -1 then player:Kick("Maliciously using RemoteEvents.") return end

	if player.Character and player.Character:FindFirstChild(script.Parent.Name) then
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),math.rad(axis*60))
	else
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),0)
	end
end)

The C0 is always going to be between 1 and -1, and if the character doesn’t exist, then the server script already checks that, with an if statement saying if player.Character.

i guess what you mean is put the anticheat thing inside the if parameter that checks if the character exists. You could do that.

I don’t think you’re understanding what I’m saying.

I added a return in your anticheat check, because without that return, on this line:

Your script errors, because player.Character.Torso no longer exists since you kicked them. You can test it yourself without the return.

Error:

I tried it before on my own game and nothing errored.

I guess you can add a return.

Try adding if player.CharacterAppearanceLoaded() or something.

The player has already been kicked, which is why the error appears. CharacterAppearanceLoaded would just cause another error. Returning after the anticheat check prevents it.

I did say (optional) in the section where you add the anticheat, in my tutorial.

Just remove it. Sure, an exploiter can make their C0 orientation crazy. It doesn’t benefit them.

No there’s no need to remove it if you can fix it by adding a return. I’m not saying there’s an unsolvable problem, I’m telling you the solution.

Do as you please though, just telling you how to fix it.

1 Like

Can’t you kick the player, and then use return?

That’s how you’re supposed to do it

1 Like

Yes, that’s what my fix does.

I updated my post, kicking the player then using return on the script, so there’s no errors.

Apologies for asking, but how would you do this with r15 avatars?

Since there’s more Motor6Ds in R15, you’d have to probably alter more than just the Right Shoulder for R6.

This was a super useful post! Thanks for making this! I’ve also made some edits that people can use if they want this to only be in effect while the player is in first person. This post is where I got the first person detection from: How do I detect if a player is in first person? - Help and Feedback / Scripting Support - Developer Forum | Roblox

The code below will reset the flashlight back to the default position whenever the player is in third person.

Server:

script.Parent.Event.OnServerEvent:Connect(function(player,axis,firstperson)
	if player.Character and player.Character:FindFirstChild(script.Parent.Name) and firstperson then
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),math.rad(axis*60))
	else
		player.Character.Torso["Right Shoulder"].C0 = CFrame.new(player.Character.Torso["Right Shoulder"].C0.Position) * CFrame.Angles(0,math.rad(90),0)
	end
end)

Client:

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local head = char:WaitForChild("Head")

camera:GetPropertyChangedSignal("CFrame"):Connect(function()
	local fp = nil
	if head.LocalTransparencyModifier == 1 then
		fp = true
	else
		fp = false
	end
	
	script.Parent.Event:FireServer(camera.CFrame.LookVector.Y, fp)
end)

While working on the code, I also accidentally made a version where it doesn’t reset so you can basically set your flashlight’s Y position by going in first person and it’ll lock when going in third person. The code for that is below.

Client:

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local head = char:WaitForChild("Head")

camera:GetPropertyChangedSignal("CFrame"):Connect(function()
	if head.LocalTransparencyModifier == 1 then
		script.Parent.Event:FireServer(camera.CFrame.LookVector.Y)
	end
end)

(The server script remains unchanged for this version.)

Your notification just made me revive my devforum account and I’m back.

Also, thanks (:

1 Like

Welcome back!

1 Like

By the way, for any new people seeing this post, you should use UnreliableRemoteEvents for this, as it will be more smooth and efficient.