Flashlight Not Working as Intended

I thought I was done with this endeavor of mine, but no. It keeps coming back, telling me that I can’t, for the life of me, figure out how to fix it.

Refer back to my prev. post on this to know what I’ve been going through.

Same flashlight, different problem. It was working fine before, all the players had their set of flashlights to hold. However, the issue arises from the fact that when a player unequips/equips the flashlight while others hold their own, they all just magically disappear.

SERVERSCRIPT:

local EquipFlash = ReplicatedStorage.EquipFlash
local Flashlight = nil

EquipFlash.OnServerEvent:Connect(function(player, trigger)
	local character = player.Character or player.CharacterAdded:Wait()
	
	if trigger then
		Flashlight = ReplicatedStorage.FlashHandle:Clone()
		Flashlight.Parent = character

		local Motor6D = Instance.new("Motor6D")
		Motor6D.Parent = character
		Motor6D.Part0 = character:FindFirstChild("Right Arm")
		Motor6D.Part1 = Flashlight

		task.wait(.25)
		Flashlight.Transparency = 0
		task.wait(.925)
		Flashlight.Flash.SpotLight.Enabled = true
	else
		task.wait(0.925)
		Flashlight.Flash.SpotLight.Enabled = false

		character:FindFirstChild("Motor6D"):Destroy()
		Flashlight:Destroy()
		Flashlight = nil
	end
end)

I think the problem stems from the global values (Flashlight), but I can’t piece together what better alternative I can think of. My brain’s fried, so apologies if the answer is a bit obvious.

Also, I’m only showing the ServerScript, and will be showing the LocalScript IF it is needed :smiley:

script is very unsafe by the way.

if trigger then
if Flashlight~=nil then return end

Would make it a bit safer.
I also dont understand point of parenting motor under a character and causing network issues?

local EquipFlash = ReplicatedStorage.EquipFlash
local Flashlight:BasePart? = nil

EquipFlash.OnServerEvent:Connect(function(player:Player, trigger:boolean|any):()
	if type(trigger)~="boolean" then player:Kick("Nasty exploiter") end
	local character:Model = player.Character or player.CharacterAdded:Wait()

	if trigger then
		if Flashlight~=nil then return end
		Flashlight = ReplicatedStorage.FlashHandle:Clone()
		local Motor6D:Motor6D = Instance.new("Motor6D")
		Motor6D.Part0 = character:FindFirstChild("Right Arm")
		Motor6D.Part1 = Flashlight
		Motor6D.Parent = Flashlight
		Flashlight.Parent = character
		task.wait(.25)
		Flashlight.Transparency = 0
		task.wait(.925)
		Flashlight.Flash.SpotLight.Enabled = true
	else
		if Flashlight==nil then return end
		task.wait(0.925)
		Flashlight:Destroy()
		Flashlight = nil
	end
end)

What do you mean by “unsafe”?

randomrandom

Realistically client could just pass true two times and there so will get “double flashlight” and so on.
It doesn’t even have to be an exploiter; It could just so heppen and it will heppen 100% if said player is an exploiter.

local character:Model = player.Character or player.CharacterAdded:Wait()
Is also unsafe too
I would instead do:

local character:Model? = player.Character
if character==nil then return end

Well, it isn’t really a game aimed for a large audience. Though I am curious, what does that do?
(pardon my lack of coding knowledge :pray:)

Writing unsafe code is never an execuse especially in a high level language like Luau.

Treat all arguments except player in OnServerEvent to be unsafe.
player.Character or player.CharacterAdded:Wait() for example, would create an insane amount of yielded threads that will wait for a moment to resume (the consequences will be terrifying when it happens, aka when the player gains character back).
If your code is unsafe, then it’s 100% your fault, and no excuses can cover it.
Your code had 2 vulnerabilities that would’ve let an exploiter entirely crash the game (yes, really, no overexaggerating that).

Hmm, I’ll take note of that when I make bigger games. This is only a test run for something I’m planning to make in the long run, so thank you for bringing that up to my attention.

But I do believe we’re straying far from the actual topic? (or am I just not understanding stuff correctly)

I gave reply towards all information you privided.

If there is an issue then its not with server most likely

I could give more if it’s needed, but I’ll test out the current code and I’ll update in a bit.

:skull: ok istg i cant speak that corporate ahh:
You aren’t firing remote event and that the entire problem

pretty sure the problem is you have a local variable which defines the flashlight outside of the event which would only work if it was a client sided script

so it’s a global variable that can be accessed anywhere within that script

if you want each player to have a separate flashlight with separate events/states you gotta create a sort of imaginary variable

you are wrong.
That not a global variable.
Scope variable (in this case closure) and global variable is 2 different things that you should never confuse.
Edit:
Maybe but yet again no context at all and OP’s refusing to fix all security in code just making it more confusing.

1 Like

im not familiar with this scope variable thing but how does the server script know who’s flashlight is who

if trigger then
if Flashlight~=nil then return end
Flashlight = ReplicatedStorage.FlashHandle:Clone()
local Motor6D:Motor6D = Instance.new(“Motor6D”)
Motor6D.Part0 = character:FindFirstChild(“Right Arm”)
Motor6D.Part1 = Flashlight
Motor6D.Parent = Flashlight
Flashlight.Parent = character
task.wait(.25)
Flashlight.Transparency = 0
task.wait(.925)
Flashlight.Flash.SpotLight.Enabled = true
else
if Flashlight==nil then return end
task.wait(0.925)
Flashlight:Destroy()
Flashlight = nil
end

since it’s a server script if someone else were to do Flashlight:Destroy() wouldnt it break

That’s the point I’m getting at. I see the problem but I just don’t know how to fix it :pray:

Why you didnt told me that earlier? :skull:
Ok 1 sec gotta edit the post with fixed code

local EquipFlash = ReplicatedStorage.EquipFlash
local Handle = ReplicatedStorage.FlashHandle
local FL_Name:string = Handle.Name

EquipFlash.OnServerEvent:Connect(function(player:Player, trigger:boolean|any):()
	if type(trigger)~="boolean" then player:Kick("Nasty exploiter") end
	local character:Model? = player.Character
	if character==nil then return end

	if trigger then
		if character:FindFirstChild(FL_Name)~=nil then return end
		local Flashlight:BasePart? = Handle:Clone()
		local Motor6D:Motor6D = Instance.new("Motor6D")
		Motor6D.Part0 = character:FindFirstChild("Right Arm")
		Motor6D.Part1 = Flashlight
		Motor6D.Parent = Flashlight
		Flashlight.Parent = character
		task.wait(.25)
		Flashlight.Transparency = 0
		task.wait(.925)
		Flashlight.Flash.SpotLight.Enabled = true
	else
		local Flashlight:BasePart? = character:FindFirstChild(FL_Name)
		if Flashlight == nil then return end
		Flashlight:Destroy()
	end
end)
1 Like

I thought it was clear in my original post :pray: mb on that

I’ll go test it out with the new code.

Gonna go test it out with some people tomorrow. Thanks so much :pray:

Learned a few things today lolll

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