Whats another way i can get player in a server script

the sliding system works fine but its the damage part im having a problem with. as you see in the video it creates a part on both players instead of the player who triggered(Its not letting me upload the video) script: local SlideDamage = game.StarterPlayer.StarterCharacterScripts.LocalScript.SlideDamage

local players = game:GetService(“Players”)

game.Players.PlayerAdded:Connect(function(player)

local character player.CharacterAdded:Wait()



local function Damage()
	
	
	
	
local part = Instance.new("Part")
	part.Parent = player.Character.HumanoidRootPart
	part.Size = Vector3.new(5,5,5)
	part.CanCollide = false
	part.Color = Color3.new(1, 0, 0)
	part.Transparency = 0.79
	part.CanTouch = true
	part.Name = "Part22"

	
	local weld = Instance.new("Weld")
	weld.Parent = part
	weld.Part0 = part
	weld.Part1 = player.Character.HumanoidRootPart
	weld.C0 = CFrame.new(0,0.4,0)
	wait(0.1)
	print("Damage Enemy")
	db = true
	
	
	local RightArm = player.Character:FindFirstChild("[Right Arm]")
	local LeftArm = player.Character:FindFirstChild("[Left Arm]")
	local head = player.Character:FindFirstChild("Head")
	local Torso = player.Character:FindFirstChild("Torso")
	local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
	local LeftLeg = player.Character:FindFirstChild("[Left Leg]")
	local RightLeg = player.Character:FindFirstChild("[Right Leg]")
	
	
	part.Touched:Connect(function(hit)
		
			
		
		
		if Torso or HumanoidRootPart or RightLeg or LeftLeg or head or LeftArm or RightArm then
			
			if hit.Parent:FindFirstChild("Torso") or hit.Parent:FindFirstChild("HumanoidRootPart") and hit.Parent ~= player.Character then
				
				hit.Parent.Torso.Parent.Humanoid.Health = hit.Parent.Torso.Parent.Humanoid.Health -3
				

			else
				print("not a torso")
				return
			end
			
	
	
			
			
		end
		wait(0.5)
		print(hit)
		print("Damage")
		db2 = true
	end)
	
	
	wait(1)
	weld:Destroy()
	part:Destroy()

end

SlideDamage.OnServerEvent:Connect(Damage)

	
end)
1 Like

The code is kinda messy, what are you trying to achieve?

What I notice, you want a client to Fire a remote to server, server should create a part in the character of that player, and the part should be connected to a touch event to deal damage.

But, theres not need to use PlayerAdded event to create a function from that event, and you are creating the event listener inside that function too which is not needed.

Check this example. The Remote its placed in ReplicatedStorage. The server script has the listener created when script runs by first time, its listening when a client fires it. Then when a client fires the event, server perform a function to create the part, weld it and connect the touch event, then when touch event is triggered perform a check if its a real player or an NPC and deal the damage. It contains a debounce/lock so it would trigger only once after touch and restore the debounce after 1 second:

local SlideDamage = game.ReplicatedStorage:WaitForChild("SlideDamage")

local Players = game:GetService("Players")

local function Damage(player)
	warn("damage function called from a client", player)
	local part = Instance.new("Part")
	part.Size = Vector3.new(5,5,5)
	part.CanCollide = false
	part.Color = Color3.new(1, 0, 0)
	part.Transparency = 0.79
	part.CanTouch = true
	part.Name = "Part22"
	part.Parent = player.Character.HumanoidRootPart

	local weld = Instance.new("Weld")
	weld.Part0 = part
	weld.Part1 = player.Character.HumanoidRootPart
	weld.C0 = CFrame.new(0,0.4,0)
	weld.Parent = part
	
	print("Part created and parented")
	
	local debounce = false
	part.Touched:Connect(function(hit)
		if not debounce then
			-- Check if its a real player
			local playerHit = Players:GetPlayerFromCharacter(hit.Parent)
			
			if playerHit then
				if playerHit ~= player then
					warn("player touched dealing damage")
					debounce = true
					playerHit.Character.Humanoid.Health -= 10
					-- Restore debounce
					task.delay(1, function() debounce = false warn("debounce:", debounce) end)
				end
			else
				if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("HumanoidRootPart") and hit.Parent:FindFirstChild("Humanoid") then
					warn("NPC touched dealing damage")
					debounce = true
					hit.Parent.Humanoid.Health -= 10
					-- Restore debounce
					task.delay(1, function() debounce = false warn("debounce:", debounce) end)
				end				
			end
		end
	end)
end
SlideDamage.OnServerEvent:Connect(Damage) -- The event listening clients and calling Damage function
1 Like

well, what I’m trying to achieve that the part only spawns on the player who triggered but it spawns on every player when a person triggers it the keycode is y. So i messed around with the script a bit but now the the keycode wont trigger heres the userinput script: local humanoid = script.Parent:WaitForChild(“Humanoid”)
local anim = humanoid:LoadAnimation(script:WaitForChild(“SlideAnimation”))
local anim2 = humanoid:LoadAnimation(script:WaitForChild(“Animation”))

local players = game:GetService(“Players”)
local player = players.LocalPlayer

local UIS = game:GetService(“UserInputService”)
local SlideDamage = game.ReplicatedStorage.LocalScript.SlideDamage

local debounceTime = 2.50 – set debounce time to half a second
local lastInputTime = tick() – initialize last input time to current time

local slidingPart = player.Character.HumanoidRootPart.SlidingClone:GetChildren()

local SlideSound = game.Workspace.Sounds:GetChildren()

local slideL = game.Workspace.Sliding

local slideLl = game.Workspace.Sliding2

local slideLlS = game.Workspace.PlaySlideSound

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
if input.KeyCode == Enum.KeyCode.Y then

		local running --bool to determine if player is running

		humanoid.Running:connect(function(speed) -- return value; speed of the running
			if speed > 0 then running = true
			elseif speed < 1 then
				running = false
			
			end
		end)
		local currentTime = tick()
		
		if currentTime - lastInputTime >= debounceTime then -- check if debounce time has passed
			
			lastInputTime = currentTime -- update last input time
			player.Character.Humanoid.WalkSpeed = 50
			SlideDamage:FireServer(player)
			anim:Play()
			slideL:FireServer()
			--for i, v in pairs(slidingPart) do
				--print(v.Name)
			--	v.Enabled = true
		--	end
			slideLlS:FireServer(player)
		--	for i, v in pairs(SlideSound) do
		--		print(v.Name)
			--	v:Play()
			--end
			while wait() do
				player.Character.Humanoid.WalkSpeed = player.Character.Humanoid.WalkSpeed - 1

				if player.Character.Humanoid.WalkSpeed == 5 then
					
					anim:Stop()
					player.Character.Humanoid.WalkSpeed = 17
							break
							
				end
			end
		end
	end
		end

end)

UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Y then
player.Character.Humanoid.WalkSpeed = 17
anim:Stop()
slideLl:FireServer(player)
–for i, v in pairs(slidingPart) do
– print(v.Name)
– v.Enabled = false
– end
end
end)

(im not good at organizing stuff)

Well, test the code I sent, it works, you just need to add the rest of your system

So, i reread. what you said and it works thank you, I’m having the same problem with my trail, but I’ll just look down on the code you typed.

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