Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Mouse.Target refers to the player and not the target?

When I was testing my gun, it damaged me than the person that I was shooting, Why? Any way to fix it?
Gun script:

local debounce = false
script.Parent.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function(idk)
		if debounce then else
			******************:FireServer(mouse.Target.Parent)
			debounce = true
			wait(1)
			debounce = false
		end
	end)
end)

RemoteEvent script:

script.Parent.OnServerEvent:Connect(function(key)
	if workspace[key.Name]:FindFirstChild("Humanoid") then
		workspace[key.Name].Humanoid:TakeDamage(10)
	end
end)

Thank you in advance!

1 answer

✓ Accepted answer

Your issue is on line 1 of the remote event script. When talking from client to server the first parameter passed is the player who actually fired the server. So it's reading Key as yourself because it's in the first parameter's position. The fix is easy. Change the first line to this instead:

script.Parent.OnServerEvent:Connect(function(plr, key)

6 comments

0

So, now it has 2 arguments. Which one is passed off as the one who got shot? I used "plr" and it didn't fix it.

0

Key is still the one that got shot. When you go from client to server your first parameter for OnServerEvent is always the player who fired the script, so your key that you passed from the local script is in the second slot where Key is now. The way you had it before with Key in the first slot, it was reading Key as the player that called the script, rather than the passed parameter.

0

"attempt to index local 'key' (a nil value)"

0

I saw my error: I didn't tell Lua the mouse's target.

0

With my error being fixed, thank you!

0

Np

This discussion is preserved as it was. New answers, comments, and votes are closed.