Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Soccer Ball Stealing System?

Asked by
Rynappel 212 Moderation Voter
5 years ago
Edited 5 years ago

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:

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

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

1Hierarchy
2    ServerStorage
3        Ball
4            TheScript

(SerpentineKing you might remember this)

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Your code:

1script.Parent.Touched:connect(function(player)
2    if player:FindFirstChild("Humanoid") then
3        for _,v in pairs(game.Players:GetPlayers()) do
4        v.Backpack:ClearAllChildren()
5        wait (0.1)
6        ball:clone().Parent = player.Backpack
7        end
8    end
9end)

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

1player: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.

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

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:

01--Make sure the script is inside the Handle of the ball
02debounce = false
03script.Parent.Touched:Connect(function(part)
04    local player = game.Players:FindFirstChild(part.Parent.Name)
05    if player and debounce == false then
06        debounce = true
07        ball.Parent = player.Character
08        wait(1) -- time before the ball can be taken again
09        debounce = false
10    end
11end)

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.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

01local debounce = false
02 
03handle.Touched:Connect(function(hit)
04    if hit and hit.Parent and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
05        if hit.Parent ~= handle.Parent.Parent and debounce == false then
06            debounce = true
07            handle.Parent.Parent = hit.Parent
08            wait(1)
09            debounce = false
10        end
11    end
12end)

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

Revised Server Script

01local repst = game:GetService("ReplicatedStorage")
02local remote = Instance.new("RemoteEvent")
03remote.Name = "FootballEvent"
04remote.Parent = repst
05 
06local debounce = false
07 
08remote.OnServerEvent:Connect(function(player, event, tool)
09    if event == "Attach" then
10        local char = player.Character
11        local root = char:WaitForChild("HumanoidRootPart")
12 
13        local handle = repst:WaitForChild("Handle"):Clone()
14 
15        local weld = Instance.new("Weld")
View all 39 lines...
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? Rynappel 212 — 5y
0
Also you forgot to do the Handle variable in the Revised Code script :) thats fine tho Rynappel 212 — 5y

Answer this question