Calling function for a specific person thru remote event

hello, so my issue here is that i gotta press a button and then it should call the functions for a specific person (here, i put my own user). but it says " ServerScriptService.AuraHandler:69: attempt to index nil with ‘Character’ " (AuraHandler is the module script). here are the scripts:

LOCALSCRIPT IN THE BUTTON

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Remotes.AuraUnequipAll:FireServer(game.Players.Kyhomi)
	wait(0.1)
	game.ReplicatedStorage.Remotes.AuraEquip:FireServer(game.Players.Kyhomi, game.ReplicatedStorage.Auras.Unique) --Unique is the name of the aura and Kyhomi is my username
end)

MODULE SCRIPT IN SERVERSCRIPTSERVICE

--i only put the functions but the rest (local module, return module... are just hidden)

function AuraHandler.EquipAura(player, char, targetplr, aura)
	local targetchar = targetplr.Character
	if not targetplr or not targetchar or not aura then
		warn("Missing parameters.")
		return
	end
	
	local Auras = rs:FindFirstChild("Auras")
	if not Auras then return end
	
	if not Auras:FindFirstChild(aura.Name) then
		warn(aura.Name .. " wasn't found in ReplicatedStorage.")
		return
	end
	
	local aurafolder = targetchar:FindFirstChild("Aura")
	if not aurafolder then
		aurafolder = Instance.new("Folder", targetchar)
		aurafolder.Name = "Aura"
	end
	
	if #aurafolder:GetChildren() > 0 then
		warn("The aura: " .. aurafolder:GetChildren()[1].Name .. " is already equipped.")
		return
	end
	
	
	local auraclone = Auras[aura.Name]:Clone()
	auraclone.Parent = aurafolder
	
	local function cloneAndWeldParts(sourcePart, targetPart)
		for _, part in ipairs(sourcePart:GetChildren()) do
			if part:IsA("BasePart") then
				local correspondingPart = targetPart:FindFirstChild(part.Name)
				if correspondingPart then
					local auraPartClone = part:Clone()
					auraPartClone.Parent = correspondingPart
					auraPartClone.CFrame = correspondingPart.CFrame
					
					local weld = Instance.new("WeldConstraint")
					weld.Part0 = correspondingPart
					weld.Part1 = auraPartClone
					weld.Parent = correspondingPart
				end
			elseif part:IsA("Model") then
				local correspondingPart = targetPart:FindFirstChild(part.Name)
				if correspondingPart then
					cloneAndWeldParts(part, correspondingPart)
				end
			end
		end
	end
	
	cloneAndWeldParts(auraclone, targetchar)
	
	
	if not aurafolder:FindFirstChild(aura.Name) then
		local value = Instance.new("StringValue", aurafolder)
		value.Name = aura.Name
	end
	
	Animation(targetplr, targetchar, {Name = aura.Name})
	Sound(targetplr, targetchar, {Name = aura.Name})
	
	warn(aura.Name .. " equipped to " ..targetchar.Name)
end




function AuraHandler.UnequipAll(player, char, targetplr)
	local targetchar = targetplr.Character
	if targetplr and targetchar then
		local aurafolder = targetchar:FindFirstChild("Aura")
		if aurafolder then
			local auraname = aurafolder:FindFirstChildWhichIsA("Folder")

			if activeAnimations[targetplr.UserId] then
				for auraName, animationtrack in pairs(activeAnimations[targetplr.UserId]) do
					animationtrack:Stop()
				end
				activeAnimations[targetplr.UserId] = nil
			end

			local soundinstance = targetchar:FindFirstChild("HumanoidRootPart"):FindFirstChild("AuraSound")
			if soundinstance then
				soundinstance.SoundId = ""
				soundinstance.RollOffMaxDistance = 0
				soundinstance.Volume = 0
				soundinstance.TimePosition = 0
			end

			for _, child in ipairs(targetchar:GetChildren()) do
				for _, secondChild in ipairs(child:GetChildren()) do
					if secondChild.Name == child.Name then
						secondChild:Destroy()
					end
				end
			end
			
			warn("The aura: " .. auraname.Name .. " was successfully destroyed.")
			auraname:Destroy()
		end
	end
end

SERVER SCRIPT IN SERVERSCRIPTSERVICE

--remotes is a folder in replicatedstorage with the remote events

remotes.AuraEquip.OnServerEvent:Connect(function(player, char, targetplr, aura)
	local char = player.Character

	auramodule.EquipAura(player, char, targetplr, aura)
end)

remotes.AuraUnequipAll.OnServerEvent:Connect(function(player, char, targetplr)
	local char = player.Character

	auramodule.UnequipAll(player, char, targetplr)
end)

thanks for ur help

Which line is the 69 in the module?
ServerScriptService.AuraHandler:69

local targetchar = targetplr.Character

If you want to call a remote event for a specific player then you can do:

RemoteEvent:FireClient(player,...)

Where RemoteEvent is your specified Remote Event and the player is the specific person you want and ... indicates any other information that you want to send.

that could work but im calling it from the localscript in the button

Then you can use BindableEvents. However, they aren’t really recommended due to being unreliable.

I dont understand what you want to achieve. You are firing this remote sending a player to the server:

game.ReplicatedStorage.Remotes.AuraUnequipAll:FireServer(game.Players.Kyhomi)

You are receiving the call in server, and you are getting 2 players, who fired the remote and the “target” player
remotes.AuraUnequipAll.OnServerEvent:Connect(function(PlayerWhoFiredIt, TARGET_Player)

You are doing it like this:

remotes.AuraUnequipAll.OnServerEvent:Connect(function(player, char, targetplr)
	local char = player.Character
	auramodule.UnequipAll(player, char, targetplr)
end)

You are creating again a variable named “char” when a parameter named “char” exist too, which holds the TARGET, so, you are replacing the target with the original player that fired the remote when you do this: local char = player.Character
And sending that to the module: auramodule.UnequipAll(player, char, targetplr)

In this line: remotes.AuraUnequipAll.OnServerEvent:Connect(function(player, char, targetplr)
targetplr doesnt even exist, cause you only sent 1 parameter, so you cant send something that is nil:
auramodule.UnequipAll(player, char, targetplr)

alr ill try to use them thanks

wait so what should i do in that case, i should remove the char variable?

I would recommend change that variable name yes, just to not get confused with the parameter. But the problem is this:

Client only sends 1 param, a target player:
game.ReplicatedStorage.Remotes.AuraUnequipAll:FireServer(game.Players.Kyhomi)

So server only gets 2 params, the original player that fired the remote as first param, and the second param is what you sent (game.Players.Kyhomi)
So you are receiving this:
remotes.AuraUnequipAll.OnServerEvent:Connect(function(originalPlayer, Target, nil)

ohh so the game thinks game.Players.Kyhomi is the char?

Do a change like this: (you only have 2 params to work with, not 3)

remotes.AuraUnequipAll.OnServerEvent:Connect(function(originalPlayer, PlayerSENT)
	local whoFiredTheRemote = originalPlayer
	local targetPlayer_CHAR = PlayerSENT.Character
	
	auramodule.UnequipAll(whoFiredTheRemote, targetPlayer_CHAR)
end)

Obviously change the logic in your module too, cause you are not sending 3 params, only 2
Change this:
function AuraHandler.UnequipAll(player, char, targetplr)
To this:
function AuraHandler.UnequipAll(originalPlayer, playerSent)

And modify the logic you used in the function to use those params

thanks, ill try that right after since im scripting a saving thing rn

yoo i think its working so im gonna connect my alt to try it

yooo thank u so much it worked

1 Like