How to make script detect selected player/userid?

I have an ability that uses a tool given to the player, but the issue is I only want the tool to be given to me, and no one else.

It gives it to everyone right now, I want to know is there is a way to modify this server script so that it only gives it to me via looking for a specific player name or user id.

local effectsContainer = workspace:FindFirstChild("EFFECTS CONTAINER") or Instance.new("Folder")
effectsContainer.Name = "EFFECTS CONTAINER"
effectsContainer.Parent = workspace

local events = game.ReplicatedStorage:WaitForChild("Events")
local fireRE = events:WaitForChild("FireBezierCurveAttack")
local displayRE = events:WaitForChild("DisplayBezierCurveAttack")

local rnd = Random.new()


function onFired(plr: Player, camCF: CFrame, mouseCF: CFrame)
	
	local char = plr.Character
	if not char then return end
	
	local equippedTool = char:FindFirstChildOfClass("Tool")
	if not equippedTool or not equippedTool:FindFirstChild("ATTRIBUTES") or not equippedTool.ATTRIBUTES:FindFirstChild("BEZIER_CURVE_ATTACK") then return end
	
	local hum = char:WaitForChild("Humanoid")
	if not hum or hum.Health <= 0 then return end
	
	local attributes = equippedTool.ATTRIBUTES
	if attributes.ON_COOLDOWN.Value == true then return end
	
	attributes.ON_COOLDOWN.Value = true
	
	local root = hum.RootPart
	
	local rp = RaycastParams.new()
	rp.FilterType = Enum.RaycastFilterType.Exclude
	rp.FilterDescendantsInstances = {effectsContainer, char}
	
	local origin = camCF.Position
	local range = attributes.MAX_RANGE.Value + (origin - root.Position).Magnitude
	local dir = (mouseCF.Position - origin).Unit * range
	
	local ray = workspace:Raycast(origin, dir, rp)
	local endPos = ray and ray.Position or origin + dir
	
	local charRay = workspace:Raycast(root.Position, (endPos - root.Position), rp)
	endPos = charRay and charRay.Position or endPos
	
	local numProjectiles = rnd:NextNumber(attributes.MIN_PROJECTILES.Value, attributes.MAX_PROJECTILES.Value)
	local projectileInformation = {}
	
	local maxOffset = attributes.MAX_EXPLOSION_OFFSET.Value
	
	for i = 1, numProjectiles do
		
		local projectileEndPos = endPos + Vector3.new(rnd:NextNumber(-maxOffset, maxOffset), rnd:NextNumber(-maxOffset, maxOffset), rnd:NextNumber(-maxOffset, maxOffset))
		
		local explosionCharRay = workspace:Raycast(root.Position, (projectileEndPos - root.Position), rp)
		projectileEndPos = explosionCharRay and explosionCharRay.Position or projectileEndPos
		
		local dist = (root.Position - projectileEndPos).Magnitude
		local avgSpeed = attributes.AVERAGE_PROJECTILE_SPEED.Value
		local timeToExplode = dist / avgSpeed
		
		table.insert(projectileInformation, {endPos = projectileEndPos, timeToExplode = timeToExplode})
		
		task.spawn(function()
			
			task.wait(timeToExplode + 0.3)

			for _, desc in pairs(workspace:GetDescendants()) do
				
				if desc:IsA("Humanoid") then
					
					local distance = (desc.RootPart.Position - projectileEndPos).Magnitude
					if distance <= attributes.EXPLOSION_RADIUS.Value then
											
						local hitRP = RaycastParams.new()
						hitRP.FilterType = Enum.RaycastFilterType.Exclude
						hitRP.FilterDescendantsInstances = {desc.Parent, workspace:WaitForChild("EFFECTS CONTAINER")}

						local rootPos = desc.RootPart.Position

						local hitRay = workspace:Raycast(origin, (projectileEndPos - rootPos), hitRP)

						if not hitRay or hitRay.Instance.Parent == desc.Parent or hitRay.Instance.Parent.Parent ~= desc.Parent then
							desc:TakeDamage(attributes.DAMAGE_PER_PROJECTILE.Value)
						end
					end
				end
			end
		end)
	end
	
	displayRE:FireAllClients(root, projectileInformation, equippedTool)
	
	task.wait(attributes.COOLDOWN_LENGTH.Value)
	attributes.ON_COOLDOWN.Value = false
end

fireRE.OnServerEvent:Connect(onFired)

This should work. Apologies for messy code.

local effectsContainer = workspace:FindFirstChild("EFFECTS CONTAINER") or Instance.new("Folder")
effectsContainer.Name = "EFFECTS CONTAINER"
effectsContainer.Parent = workspace

local events = game.ReplicatedStorage:WaitForChild("Events")
local fireRE = events:WaitForChild("FireBezierCurveAttack")
local displayRE = events:WaitForChild("DisplayBezierCurveAttack")

local rnd = Random.new()


function onFired(plr: Player, camCF: CFrame, mouseCF: CFrame)
	
	if plr.UserId == 000000 --[[USER ID HERE]] then
               local char = plr.Character
	if not char then return end
	
	local equippedTool = char:FindFirstChildOfClass("Tool")
	if not equippedTool or not equippedTool:FindFirstChild("ATTRIBUTES") or not equippedTool.ATTRIBUTES:FindFirstChild("BEZIER_CURVE_ATTACK") then return end
	
	local hum = char:WaitForChild("Humanoid")
	if not hum or hum.Health <= 0 then return end
	
	local attributes = equippedTool.ATTRIBUTES
	if attributes.ON_COOLDOWN.Value == true then return end
	
	attributes.ON_COOLDOWN.Value = true
	
	local root = hum.RootPart
	
	local rp = RaycastParams.new()
	rp.FilterType = Enum.RaycastFilterType.Exclude
	rp.FilterDescendantsInstances = {effectsContainer, char}
	
	local origin = camCF.Position
	local range = attributes.MAX_RANGE.Value + (origin - root.Position).Magnitude
	local dir = (mouseCF.Position - origin).Unit * range
	
	local ray = workspace:Raycast(origin, dir, rp)
	local endPos = ray and ray.Position or origin + dir
	
	local charRay = workspace:Raycast(root.Position, (endPos - root.Position), rp)
	endPos = charRay and charRay.Position or endPos
	
	local numProjectiles = rnd:NextNumber(attributes.MIN_PROJECTILES.Value, attributes.MAX_PROJECTILES.Value)
	local projectileInformation = {}
	
	local maxOffset = attributes.MAX_EXPLOSION_OFFSET.Value
	
	for i = 1, numProjectiles do
		
		local projectileEndPos = endPos + Vector3.new(rnd:NextNumber(-maxOffset, maxOffset), rnd:NextNumber(-maxOffset, maxOffset), rnd:NextNumber(-maxOffset, maxOffset))
		
		local explosionCharRay = workspace:Raycast(root.Position, (projectileEndPos - root.Position), rp)
		projectileEndPos = explosionCharRay and explosionCharRay.Position or projectileEndPos
		
		local dist = (root.Position - projectileEndPos).Magnitude
		local avgSpeed = attributes.AVERAGE_PROJECTILE_SPEED.Value
		local timeToExplode = dist / avgSpeed
		
		table.insert(projectileInformation, {endPos = projectileEndPos, timeToExplode = timeToExplode})
		
		task.spawn(function()
			
			task.wait(timeToExplode + 0.3)

			for _, desc in pairs(workspace:GetDescendants()) do
				
				if desc:IsA("Humanoid") then
					
					local distance = (desc.RootPart.Position - projectileEndPos).Magnitude
					if distance <= attributes.EXPLOSION_RADIUS.Value then
											
						local hitRP = RaycastParams.new()
						hitRP.FilterType = Enum.RaycastFilterType.Exclude
						hitRP.FilterDescendantsInstances = {desc.Parent, workspace:WaitForChild("EFFECTS CONTAINER")}

						local rootPos = desc.RootPart.Position

						local hitRay = workspace:Raycast(origin, (projectileEndPos - rootPos), hitRP)

						if not hitRay or hitRay.Instance.Parent == desc.Parent or hitRay.Instance.Parent.Parent ~= desc.Parent then
							desc:TakeDamage(attributes.DAMAGE_PER_PROJECTILE.Value)
						end
					end
				end
			end
		end)
	end
	
	displayRE:FireAllClients(root, projectileInformation, equippedTool)
	
	task.wait(attributes.COOLDOWN_LENGTH.Value)
	attributes.ON_COOLDOWN.Value = false
     end
end

fireRE.OnServerEvent:Connect(onFired)

It does work, but I gave you the wrong script, haha.

local tool = script.Parent
local attributes = tool:WaitForChild("ATTRIBUTES")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local events = game.ReplicatedStorage:WaitForChild("Events")
local fireRE = events:WaitForChild("FireBezierCurveAttack")


tool.Activated:Connect(function()
	
	if attributes.ON_COOLDOWN.Value == false then
		
		local camCF = cam.CFrame
		local mouseCF = mouse.Hit
		
		
		fireRE:FireServer(camCF, mouseCF)
	end
end)

This one is the correct script, it was supposed to disable the tool for others. :sweat:

Try this:

local tool = script.Parent
local attributes = tool:WaitForChild("ATTRIBUTES")

local cam = workspace.CurrentCamera

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local events = game.ReplicatedStorage:WaitForChild("Events")
local fireRE = events:WaitForChild("FireBezierCurveAttack")


tool.Activated:Connect(function()
	
	if attributes.ON_COOLDOWN.Value == false and plr.UserId == 000000 --[[USERID OF PLAYER]] then
		
		local camCF = cam.CFrame
		local mouseCF = mouse.Hit
		
		
		fireRE:FireServer(camCF, mouseCF)
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.