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

How would I get the local player from a remote Event?

So, I'm writing a script that when a player talks to a NPC it gives the player a custom item, and the talking script happens in a local script so i'm using a remote event to transfer it into a Server side script because the knife wont show up if given by Local Script, but When a player talks to the NPC they just receive a number of knives equal to the amount of players in a server, if that makes sense. I just want the player who talked to the NPC to receive one knife, here's my code.

local RM = game.ReplicatedStorage:WaitForChild("GiveKnife")

local function GiveKnife(player)
	print(player)
	local knife = game.ReplicatedStorage.Knife:Clone()
	knife.Parent = player.Backpack
	wait(1)
end

RM.OnServerEvent:Connect(GiveKnife)


if you need clarification lmk and ill edit the post and respond to you

Edit: The player IS receiving the knives just fine, but they receive knives relevant to how many players there are in the server, for example if there is one player they receive one knife, if there are 10 they will receive 10 knives. I want to prevent this from happening.

This is the full local script, the RE fires from the bottom of it

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.HelperMan.Head
local Npc2 = game.Workspace.ShadySeller.Head	
local RM = game.ReplicatedStorage:WaitForChild("GiveKnife")

UIS.InputBegan:connect(function(keyCode)
	if keyCode.keyCode == Enum.KeyCode.E then
		if (Npc.Position - HumanoidRootPart.Position).magnitude < 25 then
			print("Key E was pressed")
			game.Players.LocalPlayer.PlayerGui.SpokenToHelper.Enabled = true
			wait(8)
			game.Players.LocalPlayer.PlayerGui.SpokenToHelper.Enabled = false
		end
	end
end)

local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")
local Npc = game.Workspace.HelperMan.Head

UIS.InputBegan:connect(function(keyCode)
	if keyCode.keyCode == Enum.KeyCode.E then
		if (Npc2.Position - HumanoidRootPart.Position).magnitude < 25 then
			print("Key E was pressed")
			game.Players.LocalPlayer.PlayerGui.SpokenToShady.Enabled = true
			wait(6)
			game.Players.LocalPlayer.PlayerGui.SpokenToShady.Enabled = false
			RM:FireServer()
		end
	end
end)

3 comments

0

The first value returned in a RemoteEvent is the client who called it.

0

The first parameter within an event is always the player

0

Well, it looks like there's something wrong with the LocalScript which is firing, it will be more helpful if you add the part of the LocalScript which is firing. The script you mentioned above have no errors.

3 answers

You need to reference the player through the remote evet. Example:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
local knife = game.ReplicatedStorage.Knife:Clone()
knife.Parent = player.Backpack

Player will be whoever sent the remote from a localscript. Hope this helps! If it did click accept. If it didn't please tell me in the comments and Ill try and help you more.

1 comment

0

Im a little confused, Isnt that what my code is currently doing? Since I just ran the function from above rather than from the Event itself, and called player in the function?

You can't actually replicate LocalPlayer to the server. It's strictly client-only.

Likewise, when FireServer() is called from the client, it passes with the function call two arguments:

  1. The player who fired the remote event.

  2. A tuple of values (if there is something within the parenthesis of FireServer()).

Because the function already passes the player who fired the event, you'd just use that argument instead of trying to reinvent the wheel.

In your situation, you can easily loop through GetPlayers() and insert a knife for every existing player. If a player leaves the server you can remove a knife from the user's backpack (or character).

7 comments

0

How can I fix the issue of the knives duplicating? In the edit I specified this problem

0

Check the edit.

0

I wan't the opposite, The knives are duplicating in the inventory and I want the player to have only one

0

Then check to see if there's already a knife in the player's inventory. Come on, it's not that difficult.

0

How would I check that? I'm very new to scripting, I'm still learning the basics of it and I'm still trying to learn what I can and can't do with LUA

0

'if' conditional statement

0

OK, so I've written some thing that finds what's inside of the backpack, how would I check if there are multiple of the same thing?

Well, I am going to suggest an alternative way of checking duplication of tools. Well, I believe that the problem is in your LocalScript, that is firing to the Server, which results in duplication of your tool. Below is a sample code, with explanations in it, which checks the number of an object in the Backpack and reduce it to 1.

Here, in your case, it's the knife:

--// Variables
local count = 0

--// Functions
local function toolAdded(tool)
	if tool:IsA("Tool") then -- if the instance added is a tool
		if backPack:FindFirstChild(tool.Name) then -- If another tool is found
			count += 1 -- Increase count value by 1
			if count >= 2 then -- If count is equal to, or greater than 2 
				wait() -- For safety
				tool:Destroy() -- Destroys the tool
			end
		end
	end
end

local function toolRemoved(tool)
	if tool:IsA("Tool") then
		if tool.Name == "Knife" then -- if the tool, that is removed is called Knife then [Change it to your tool's name]
			count -= 1 -- Reduces count by 1
		end
	end
end

local function OnAdded(player)
	local backPack = player:WaitForChild("Backpack") -- Gets the player's backpack
	
	--// Main
	backPack.ChildAdded:Connect(toolAdded)
	backPack.ChildRemoved:Connect(toolRemoved)
end

--// Main
game.Players.PlayerAdded:Connect(OnAdded)

Please do edit your question, with the part of the Local script, where you are firing to the server. There might be multiple firing, which results in duplication of the tool.

Lemme know if it helps!

3 comments

0

Ok, I've edited and added the local script to the question

0

Well, the local script doesn't indicate the repeat firing of the Remote Event, maybe there might be other scripts indicating it. So, I recommend you to use above code. Also, if its getting added on the amount of players, open Find in the command bar and search game.Players:GetPlayers().

0

And then results whether the found scripts have anything linked to firing to the server.

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