Server script have to be used by only one player

Hello, i made a script where when i touch a black part, i get the possibility to shoot blocks by clicking.
Almost everything works like i want but, the only problem is that when i touch that black part, everyone have the possibility to shoot blocks, not only me.

-- Local script

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local remote = game.ReplicatedStorage.RemoteEvent
local Part = game.Workspace:WaitForChild("PartToTouch")

local debounce = true
local touched = false

Part.Touched:Connect(function(plr)
	if plr.Parent:FindFirstChild('Humanoid') then
		
touched = true
end
end)



mouse.Button1Down:Connect(function()

if touched == true then

if debounce == true then
debounce = false

remote:FireServer(mouse.Hit)

wait(0.5)

debounce = true
end
end
end)

--Server Script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local Bloc = game.ReplicatedStorage.BlocTRW


RemoteEvent.OnServerEvent:Connect(function(plr, position)
	
	print(position)
	print(plr)
	
	local clone = Bloc:Clone()
	local character = plr.Character
	
	clone.CanCollide = false
	clone.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0,3,0)
	clone.Parent = workspace
	
	local direction = position.LookVector
	local forceMultipliter = 300 * clone:GetMass()
	
	clone:ApplyImpulse(direction * forceMultipliter)
	
	wait(0.3)
	
	clone.CanCollide = true
	
end)

Your client script checks if anybody touches the part, which means if someone on my client touches the black part I am going to fire to the server whenever I click, which would enable me to shoot blocks.

You need to do something like this:

local client = game.Players.LocalPlayer

Part.Touched:Connect(function(plr)
	if plr.Parent:FindFirstChild('Humanoid') then
		local p = game.Players:GetPlayerFromCharacter(plr.Parent)
		if p ~= client then return end
		
		touched = true
	end
end)

This script verifies that my client is the one who actually touches the part.

2 Likes

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