Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago

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

Log in to vote
1
Answered by 5 years ago

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)
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. ViviTheLuaKing 103 — 5y
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. XxTrueDemonxX 362 — 5y
0
"attempt to index local 'key' (a nil value)" ViviTheLuaKing 103 — 5y
0
I saw my error: I didn't tell Lua the mouse's target. ViviTheLuaKing 103 — 5y
View all comments (2 more)
0
With my error being fixed, thank you! ViviTheLuaKing 103 — 5y
0
Np XxTrueDemonxX 362 — 5y
Ad

Answer this question