Im trying to make a tool that when player 1 hits player 2 with the tool, their health gets swapped, so if player 1 had 30, and player 2 had 90, it would swap to player 1 having 90 and player 2 having 30, Ive searched online for tips on this but havent found anything explaining it.
Using a touch function, and some stored data you can achieve this. I would start on making the touch function and then I can guide you from there, Reply to this response if you want me to guide you through it
Let me rephrase that, Using a touch function to connect a remote event, that fires to the server. Then after that in the server-sided script, you can get the player who got touched Health, and the player that was touching Health. Once you have those two data points, you can just swap them out with another.
This just applies if you’re using a local script
You can achieve that using BasePart | Roblox Creator Documentation
local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local Part = script.Parent.Part
local Debounce = false
Part.Touched:Connect(function(hit)
if not Debounce then
Debounce = true
local HumanoidTouched = hit.Parent:FindFirstChild("Humanoid")
local HumanoidPlayer = player.Character:FindFirstChild("Humanoid")
if HumanoidTouched and HumanoidPlayer then
local playerTouched = game.Players:GetPlayerFromCharacter(hit.Parent)
if playerTouched ~= player then
if HumanoidTouched.Health > 0 and HumanoidPlayer.Health > 0 then
local playerHealth = HumanoidPlayer.Health
local touchedHealth = HumanoidTouched.Health
HumanoidPlayer.Health = touchedHealth
HumanoidTouched.Health = playerHealth
task.wait(1)
Debounce = false
end
end
end
end
end)
Very simple tool swapping the health of the player that gets clicked:
Script:
local Tool = script.Parent
local Signal = Tool:WaitForChild("Signal")
function HealthSwap(player1, player2)
local character1 = player1.Character
local character2 = player2.Character
if not character1 or not character2 then return end
local humanoid1 = character1:FindFirstChildWhichIsA("Humanoid")
local humanoid2 = character2:FindFirstChildWhichIsA("Humanoid")
if not humanoid1 or not humanoid2 then return end
--swapping health
local old = humanoid1.Health --storing old value
humanoid1.Health = humanoid2.Health
humanoid2.Health = old
end
--if the player has equipped the tool, the request is valid
function Validate(player)
if Tool.Parent == player.Character then
return true
else
return false
end
end
Signal.OnServerEvent:Connect(function(player, target)
if Validate(player) then
HealthSwap(player, target)
end
end)
LocalScript:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = script.Parent
local Signal = Tool:WaitForChild("Signal")
Tool.Activated:Connect(function()
local target = Mouse.Target
if not target then return end
local player2 = Players:GetPlayerFromCharacter(target.Parent)
if player2 then
Signal:FireServer(player2)
end
end)
The tool should look like this: