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

How do I transfer a player's name from client to server?

Asked by 5 years ago
Edited 5 years ago

I have been wanting to make a force choke, and it works server-side, which is excellent. There's a problem, though. When going ahead and clicking the torso of something with a humanoid in it, when the 'playchoke' event is fired, the server picks it up as my character being choked in the end. I'm confused, I believe I've done this correctly. A little support would be helpful, thanks. Here's some code from client-side and server-side:

Also, at the start of my Client-Side script, I have put the Player's Mouse's TargetFilter to nil, so it shouldn't choke my character if I'm clicking on models with humanoids:

v Client Script v


*-- Client-Side* local mouse = player:GetMouse() mouse.TargetFilter = nil mouse.Button1Down:Connect(function() if equipped == true then print('ok') if mouse.Target.Parent:FindFirstChild('Humanoid') then local mouseTarget = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) print(mouseTarget) script.playchoke:FireServer(mouseTarget) -- the choke event print('doing choke') end end end)

v Server Script v


*-- Server-Side* script.Parent.client.playchoke.OnServerEvent:Connect(function(mouseTarget) print(mouseTarget) local dead = false local damage = 15 local char = mouseTarget.Character local humanoid = char.Humanoid while true do local anim = script.Parent.Handle.victumanim local animtrack = humanoid:LoadAnimation(anim) animtrack:Play() wait(0.1) humanoid.WalkSpeed = 0 humanoid.JumpPower = 0 humanoid:TakeDamage(damage) wait(0.1) print('should be choking') end end)

In the output, on the Server-Side script, the 'print(mousetarget)' comes up with my local player's name (me) and not the victum's name I clicked. On client-side, it goes ahead and prints the player's name that I've clicked on. I'm still very confused..

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

To transfer a player, and all properties, including name to the server, the simplest way would be to use a Remote Event


Remotes


A remote event, specifically the OnServerEvent event of the remote event has an impicit player parameter (which you mistakenly used as the mouse target parameter), then all the arguments you provided when firing the remote event:

Client

local remote = --remote location here
remote:FireServer(...)

Server

local remote = --remote location here
remote.OnServerEvent:Connect(function(plr,...)

end)

Correction


So, after correcting your OnServerEvent, the result would be :

script.Parent.client.playchoke.OnServerEvent:Connect(function(plr,mouseTarget)
    print(mouseTarget)
    local dead = false
    local char = mouseTarget.Character
    local humanoid = char.Humanoid
    for i = 1,7 do
        local anim = script.Parent.Handle.victumanim
        local animtrack = humanoid:LoadAnimation(anim)
        animtrack:Play()
        wait(0.1)
        humanoid.WalkSpeed = 0
        humanoid.JumpPower = 0
        humanoid:TakeDamage(15)
        wait(0.1)
        print('should be choking')
    end
end)

FYI, you shouldn't just check whether if the mouse target has a humanoid, that could end up firing the event when you hover over an NPC, you should really check whether you can get a player from the character instead.

Hopefully This helped!

0
Thanks mate I'll try this when I get up in the morning. Do I just copy your edited server side code into mine? Jx_shii 2 — 5y
0
it isnt good practice to simply copy a script that is provided theking48989987 2147 — 5y
0
the general message is that the first parameter of OnServerEvent is the player who fired the remote theking48989987 2147 — 5y
0
Thanks so much mate, you're a legend. :D Jx_shii 2 — 5y
Ad

Answer this question