How to get LocalPlayer with touchevent?

Also if you give the parameter with touched plr it woudnt work:

You need a server script to access another player’s PlayerGui. According to the screenshots you’re using a local script.

use the former method I provided and send a request to the server.

-- local script
local players = game:GetService("Players")

BasePart.Touched:Connect(function(object)
   local player = players:GetPlayerFromCharacter(object.Parent)

   if player then
       RemoteEvent:FireServer(player) -- send a request to the server
   end
end)
-- server script in ServerScriptService

RemoteEvent.OnServerEvent:Connect(function(mainPlayer, targetPlayer)
   local ac2 = mainPlayer.PlayerGui:FindFirstChild("AttackCollection"):Clone()
   ac2.Parent = targetPlayer.PlayerGui
end)

I guess, this is what you want?

You’re calling FireClient on the touched player so only they will receive the OnClientEvent event

actually, Is it possible when the parameter goes from Localscript, to server, and then again to Localscript because I need to work with OnClientEvent but lemme test yours

It isn’t because exploiters would just use it to mess with a player’s screen (and possibly other reasons), which is why it’s server only

But you can do this:

-- server script

RemoteEvent.OnServerEvent:Connect(function(mainPlayer, targetPlayer)
   RemoteEvent:FireClient(targetPlayer)
end)
-- player's local script

RemoteEvent.OnClientEvent:Connect(function()
   -- do stuff
end)

I am working on gui it wont work

so LocalPlayer gets error wait

What does your script look like now?

do this:

local char = player.Character or player.CharacterAdded:Wait()
local hmrp = char:WaitForChild("HumanoidRootPart")

actually I cant make it from Localscript because my script would make no sense

Is there no way to do it in Server?

You can, but you can’t get the LocalPlayer without cross client-server communication meaning you would only be able to get the player that the sword touched.

You can get around this by placing a server script in StarterCharacterScripts and using

Players:GetPlayerFromCharacter(script.Parent)

LocalPlayer is not a value that is changed on the server. LocalPlayer points to who is in control of the client on the client, meaning the server will never know who is the LocalPlayer. If you want to get a player from a Part.Touched event, you would do the following.

local Part = script.Parent -- Assuming the part you want to check for is the parent of the script.

Part.Touched:Connect(function(Hit)
    local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid") -- Assuming that Hit is a limb from the player's character, you can try to find a child within that with the class of Humanoid, which tells us if there is a player or anything else such as an npc.

    if Humanoid then -- Checks if we were able to find a Humanoid, in which case it is a player/npc.
        local Character = Humanoid.Parent -- Get the parent of the Humanoid, aka the character model.
        local HitPlayer = game:GetService("Players"):GetPlayerFromCharacter(Character) -- Finds the player based off of their Character Model.
    end
end)

I hope this helps in some way.

1 Like

Your script doesnt work with my concept I am try

help
I got an idea we just need to look at the player.Name and find it later
So how can I get the player.Name and then I will take his Local.

   Combatsystem.OnClientEvent:Connect(function(plr)
        	local Players = game:GetService("Players").LocalPlayer
        	if not plr:FindFirstChild("MenuGui") then
        		local AttackCollection2 = plr.PlayerGui.AttackCollection
        		AttackCollection2.Enabled = true
        		AttackCollection2.Parent = plr.PlayerGui
        		AttackCollection2.Enabled = true
        	end
        end)

your script doesnt work it should be on Serverscriptservice