I want the MyPart to be an area which gives the player a sword upon entering and takes it away when they leave. so that its a sort of pvp only spot.
this script doesnt do it, i added prints but the prints also dont show up.
-- Find the part that the player will enter
local part = game.Workspace.MyPart
-- Create a sword object
local sword = game.ServerStorage.MySword:Clone()
print("Created sword: " .. sword)
-- Create a function to give the player the sword when they enter the part
function giveSword(object)
print("giveSword called with object: " .. object)
-- Make sure the object is a player
if object:IsA("Player") then
-- Make sure the player doesn't already have the sword
if not object.Backpack:FindFirstChild(sword.Name) then
-- Give the player the sword
sword.Parent = object.Backpack
print("Gave sword to player: " .. object)
end
end
end
-- Create a function to take the sword away from the player when they leave the part
function takeSword(player)
print("takeSword called with player: " .. player)
-- Find the sword in the player's backpack
local playerSword = player.Backpack:FindFirstChild(sword.Name)
-- If the player has the sword, remove it from their backpack
if playerSword then
playerSword.Parent = nil
print("Took sword from player: " .. player)
end
end
-- Connect the giveSword and takeSword functions to the part's "Touched" event
part.Touched:Connect(giveSword)
part.TouchEnded:Connect(takeSword)
-- Find the part that the player will enter
local part = game.Workspace.MyPart
-- Create a sword object
local sword = game.ServerStorage.MySword:Clone()
print("Created sword: " .. sword)
-- Create a function to give the player the sword when they enter the part
function giveSword(hit)
print("giveSword called with hit: " .. hit)
-- Find the player that touched the part
local player = hit.Parent
-- Make sure the player is a Humanoid
local humanoid = player:FindFirstChild("Humanoid")
if humanoid then
-- Make sure the player doesn't already have the sword
if not player.Backpack:FindFirstChild(sword.Name) then
-- Give the player the sword
sword.Parent = player.Backpack
print("Gave sword to player: " .. player)
end
end
end
-- Create a function to take the sword away from the player when they leave the part
function takeSword(hit)
print("takeSword called with hit: " .. hit)
-- Find the player that stopped touching the part
local player = hit.Parent
-- Find the sword in the player's backpack
local playerSword = player.Backpack:FindFirstChild(sword.Name)
-- If the player has the sword, remove it from their backpack
if playerSword then
playerSword.Parent = nil
print("Took sword from player: " .. player)
end
end
-- Connect the giveSword and takeSword functions to the part's "Touched" and "TouchEnded" events
part.Touched:Connect(giveSword)
part.TouchEnded:Connect(takeSword)
Here, the player variable refers to his character model, if you want the player’s backpack, you must get his player object with: game:GetService("Players"):GetPlayerFromCharacter()
But other than that, it should work.