Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Soccer Ball Stealing System?

So Im trying to make like a system where u can steal a soccer ball from somebody. So player 1 has the ball, and then player 2 walks up to player 1 and touches the ball. What I want to happen when player 2 touches the ball is: (the ball is a tool) I want player 1 to lose the tool (the ball) and player 2 to recieve the tool. But i dont know how. Here is what ive got so far:

script.Parent.Touched:connect(function(player)
	if player:FindFirstChild("Humnaoid") then
		for _,v in pairs(game.Players:GetPlayers()) do
	  	v.Backpack:ClearAllChildren() 
		wait (0.1)
		ball:clone().Parent = player.Backpack
	end
end)

Please help or tell me what I can do to improve the code! Also:

Hierarchy
	ServerStorage
		Ball
			TheScript

(SerpentineKing you might remember this)

2 answers

✓ Accepted answer

Your code:

script.Parent.Touched:connect(function(player)
    if player:FindFirstChild("Humanoid") then
        for _,v in pairs(game.Players:GetPlayers()) do
        v.Backpack:ClearAllChildren()
        wait (0.1)
        ball:clone().Parent = player.Backpack
		end
    end
end)

When the ball is touched it doesn't tag a model, it tags the part that touched it. So at line two where you put

player:FindFirstChild("Humanoid")

The machine is searching player2's body part that touched the ball for a humanoid, but the humanoid is inside the player's character model not their body part.

 for _,v in pairs(game.Players:GetPlayers()) do
        v.Backpack:ClearAllChildren()
        wait (0.1)
        ball:clone().Parent = player.Backpack
end

You don't need to remove everything from everyones backpacks or clone the ball, this could cause some problems. All you need to do is parent the ball to player2's character

New code:

--Make sure the script is inside the Handle of the ball
debounce = false
script.Parent.Touched:Connect(function(part)
	local player = game.Players:FindFirstChild(part.Parent.Name)
	if player and debounce == false then
		debounce = true
		ball.Parent = player.Character
		wait(1) -- time before the ball can be taken again
		debounce = false
	end
end)

I also added a debounce so the ball can't be taken again immediately after someone else takes it. They have to wait at least a second, this is useful or the ball might continuously loop between the two players.

Based on your previous script

Improvements

Use :GetService() to retrieve the Players service

You don't need to clear the children of the backpack as the tool may be in the player's Character (as it is when equipped)

Use :GetPlayerFromCharacter() to differentiate between players and NPCs

Issues

You're cloning the ball and parenting the clone, so the original ball is not having its parent shifted / being destroyed

Revised Code

local debounce = false

handle.Touched:Connect(function(hit)
	if hit and hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
		if hit.Parent ~= handle.Parent.Parent and debounce == false then
			debounce = true
			handle.Parent.Parent = hit.Parent
			wait(1)
			debounce = false
		end
	end
end)

debounce for cooldown, check the hit.parent is not already the tool's parent

Revised Server Script

local repst = game:GetService("ReplicatedStorage")
local remote = Instance.new("RemoteEvent")
remote.Name = "FootballEvent"
remote.Parent = repst

local debounce = false

remote.OnServerEvent:Connect(function(player, event, tool)
	if event == "Attach" then
		local char = player.Character
		local root = char:WaitForChild("HumanoidRootPart")

		local handle = repst:WaitForChild("Handle"):Clone()

		local weld = Instance.new("Weld")
		weld.Part0 = root
		weld.Part1 = handle
		weld.C0 = CFrame.new(0, -2.5, -1.5)
		weld.Parent = handle

		handle.Parent = tool

		handle.Touched:Connect(function(hit)
			if hit and hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
				if hit.Parent ~= tool.Parent and debounce == false then
					debounce = true
					tool.Parent = hit.Parent
					wait(1)
					debounce = false
				end
			end
		end)
	elseif event == "Remove" then
		local handle = tool:FindFirstChild("Handle")
		if handle then
			handle:Destroy()
		end
	end
end)

2 comments

0

So I replace the server script i already had before, with this one just above this comment? And the Revised Code script I put in the handle of the ball?

0

Also you forgot to do the Handle variable in the Revised Code script :) thats fine tho

This discussion is preserved as it was. New answers, comments, and votes are closed.