Need help with kicking a player when he's touching a tool

The script’s purpose is to heal you with a tool when you press right click, and if a random player touches the MeshPart of the tool they get kicked.
Everything works outside of the kicking, and when it does it makes a new event each time and I do not have a clue on how to make this work.
This is my attempt:

local script on the tool:

local tool = script.Parent
local equipped = false

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

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


tool.Equipped:Connect(function()
    equipped = true

end)

tool.Unequipped:Connect(function()
    equipped = false

end)

mouse.Button2Down:Connect(function()
    if not equipped then return end
    local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
    remoteEvent:FireServer()
end)

Server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

local ServerStorage = game:GetService("ServerStorage")
local tool2 = ServerStorage:FindFirstChild("CHOO CHOO") -- the Tools name and its on Server Storage because its gonna be copied to specific players

local Players = game:GetService("Players")     

remoteEvent.OnServerEvent:Connect(function(player)
    print(player.Name .. " fired")
    if player.Character then
        print('h')
        player.Character.Humanoid.Health += player.Character.Humanoid.MaxHealth/2
        local toolkick = player.Character
        local toolman = toolkick:WaitForChild("CHOO CHOO") -- don't really know if I need to do WaitForChild to get a specific name
        local function kick(otherPerson)
            local character = otherPerson.Parent
            local humanoid = character:FindFirstChildOfClass("Humanoid")
            if humanoid then
                Players:FindFirstAncestor(character):Kick()
            end
        end
         toolman.MeshPart.Touched:Connect(kick)
    end
end)
-- from here onwards its not the problem this just  makes a copy of the tool from the ServerStorage to some people

local userId = 716655375
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)	
	if player.UserId == userId or player.UserId == 211915203 or player.UserId == 337640276 then
		player.CharacterAdded:Connect(function(character)
	
			tool2:Clone().Parent = character
		end)
	end
end)

This is probably the issue as the character and the Player are not direct parent/children of each other, the character is in the workspace and the Player is in the Players service, maybe try

Players:FindFirstChild(character.Name):Kick()

So it first the first child with the name of the character name and kick them?

Edit: Actually it’s probably not working cause you’re not giving a string to make it find and the Ancestor of the Players service is the game itself

Edit 2: An alternative that can be done for anyone who finds this post and has a similar issue can be to use GetPlayerFromCharacter

Players:GetPlayerFromCharacter(character):Kick()
4 Likes

Alright I am going to try that

Oh it did work thank you so much

2 Likes

Anytime! If you have anymore issues don’t be afraid to make another post!

Edit: Yes just to confirm, even @synical4’s method will work, I mostly did FindFirstChild since I kinda zoned out for a moment and forgot about that haha, I’ll include that as an edit on my solution post

3 Likes

I see that you have a solution already:

Players:FindFirstChild(character.Name):Kick()

But I still want to show you a cool function that could help you in the future with things like this:

Players:GetPlayerFromCharacter(character):Kick()

Basically there is a function that handles this for you. The other method mentioned also works but I thought I would include this.

1 Like

Ah, thank you too! I will keep that in mind in the future

1 Like