Hello so I am making a game where you step on the platform and you equip a sword and you fight simple and it works. (I got this script off another forumn) the problem is I don't know how to add/change the script to make it where when you step off the platform your sword goes away/unequipps. this is my following script
local block = game.Workspace.MF local sword = game.ReplicatedStorage.Heal
block.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then if not hit.Parent:FindFirstChildWhichIsA("Tool") then if not game.Players[hit.Parent.Name].Backpack:FindFirstChild(sword.Name) then sword:Clone().Parent = hit.Parent print(sword.Name .. " gave to " .. hit.Parent.Name) end end end end)
MF is the name of the part that you stand on to get a sword. the script works great btw. I also have another platform on the map that does the same thing
What can I add to the script to make it so you lose your sword when you step off. I don't want to mess anything up
First, we have to detect when the player leaves this platform. Unfortunately, there is no "unTouched" event, so we have to do things on our own here. There are a few ways to go about this, and it's honestly up to you which one you use. The first would be to make a separate touchable object that removes the player's sword, positioned just outside of this platform. This can, however, be bypassed if the player finds a way to exit without touching this zone. Either way, this is what the code for that would look like, as a server script;
script.Parent.Touched:Connect(function(Part) local PartModel = Part:FindFirstAncestorWhichIsA("Model") -- gets first model that the part belongs to. With bodyparts, this will always be the character. If additional objects can touch this part, you may want to add extra conditions if PartModel:FindFirstChild("Sword") then --checks if there is a sword PartModel.Sword:Destroy() --destroys the sword end end)
Another possible solution is to use magnitude instead. This is a better option because you can include it in the script that already exists. In your touched function, include this at the end such that the script waits for the player's distance from the part to be greater than a value you specify. This option is a little more advanced than the prior one.
local magnitude = (Player.Character.PrimaryPart.Position - script.Parent.Position).magnitude while magnitude < [SPECIFIED DISTANCE] do magnitude = (Player.Character.PrimaryPart.Position -Door.Hitbox.Position).magnitude wait(0.1) end Player.Character.[SWORD NAME]:Destroy()
This will create a loop at the end of the script, pausing it entirely until the player leaves. After this loop, you can destroy the sword knowing the player has left.
If you don't quite understand the concepts here, I would advise you to learn a lot more about this using the roblox developer wiki. Additionally, this issue has probably already been solved elsewhere in some form, so be sure to do a lot of googling! It's the best way to learn.