So I couldn’t really explain the problem in the title. The problem is that I made a shop for pirate ships and it works, however each boat/ship has its own unique name and I want it so whenever the player decides to buy one it should despawn the old boat spawned by the player.
I will provide you with the scripts if needed.
Thanks in advance.
Check if there’s a boat already spawned by the player when they buy a new boat. If there is, despawn it
I did but the problem is that each boat has a unique name I even went this far as to add the name of the player infront of the boat name however I cant seem to find a way to find the previous boat spawned by the same player.
Can you show me your scripts please?
for the ship spawn or the one which tries to find the previous ship?
Both (30 letters))))))))))))))))
alright so heres the script which finds if the player has spawned boat
local RS = game:GetService(“ReplicatedStorage”)
local itemBoughtRE = RS.Remotes.PlayerRemotes:WaitForChild(“OnItemBought”)
itemBoughtRE.OnServerEvent:Connect(function(plr, itemBuying)
local char = plr.Character or plr.CharacterAdded:Wait()
if plr.PlayerData.Ship.Value == itemBuying.Name then return end
if plr.PlayerData.Beli.Value < itemBuying.Price.Value then return end
plr.PlayerData.Beli.Value = plr.PlayerData.Beli.Value - itemBuying.Price.Value
plr.PlayerData.Ship.Value = RS.BuyableShips[itemBuying.Name].Name
warn(plr.Name..plr.PlayerData.Ship.Value)
if workspace:FindFirstChild(plr.Name.."ShipTest"..plr.PlayerData.Ship.Value) then
warn(plr.Name.."ShipTest"..plr.PlayerData.Ship.Value)
workspace:FindFirstChild(plr.Name.."ShipTest"..plr.PlayerData.Ship.Value):Destroy()
end
end)
and heres the module script I use for the button which spawns the ship(the button is textbutton)
local module = {}
local reps = game.ReplicatedStorage
local shipsFolder = reps.Ships
function module.spawnShip(plr,action)
local data = plr:WaitForChild(“PlayerData”)
local ship = data.Ship
local char = plr.Character
local hrp = char:WaitForChild(“HumanoidRootPart”)
if ship.Value ~= “” then
if action == “Spawn” and hrp.Position.Y <= 38 then
if workspace:FindFirstChild(plr.Name … ship.Value) then
workspace:FindFirstChild(plr.Name … ship.Value):Destroy()
end
local ray = Ray.new(hrp.Position,hrp.CFrame.UpVector * -5)
local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char,workspace.Map.Chunk2})
if not hit then
local shipTarget = shipsFolder:FindFirstChild(ship.Value):Clone()
if shipTarget.Owner.Value == “” then
shipTarget.Owner.Value = plr.Name
local rx,ry,rz = shipTarget.PrimaryPart.CFrame:ToOrientation()
shipTarget.Name = plr.Name … tostring(ship.Value)
shipTarget:PivotTo(hrp.CFrame * CFrame.new(0,3,-15) * CFrame.fromOrientation(0,0,0))–(hrp.CFrame, 0, 0 * CFrame.fromOrientation(rx,ry,rz)
shipTarget.Parent = workspace
end
end
elseif action == “Despawn” then
if workspace:FindFirstChild(plr.Name … ship.Value) then
workspace:FindFirstChild(plr.Name … ship.Value):Destroy()
end
end
end
end
return module
So what you want to achieve is:
When a player either SPAWNS or BUYS a new boat, the old boat that is already spawned should despawn
Yea said in short terms. I did successfully achieve that when you click on the spawn button it checks that if a boat is spawned it’ll despawn it and if its not found inside the Workspace it spawns it. However I want to make it so whenever the player buys a different boat it checks if theres a previous boat by the same player and destroys it.
When the player buys a different boat, can’t you just check if there’s already another boat the same way you checked when the player decides to spawn a boat?
Now you see thats the actual problem. The method I used to check if theres another boat inside the button won’t work because each boat has a different and unique name and it changes the value in the playerdata as well. So for example, the player spawns ship1 and goes to buy ship2, how will I check the previous boat if it has a different name?
The only alternative I can think of is to put every single available boat’s name and make it so the script will check if theres a boat with the same name. But it won’t be the most effective way.
Why would you need to check precisely what the previous boat was? All you need to know is if there was a boat before then destroy it.
You mean to check if there’s a boat on the name of the player?
Cause if I dont specify which boat it might destroy other players boats inside the workspace.
But what if two players have the same type of boat spawned? Can’t have a system like that then
I’m really not sure how you’ve made this system, but how about naming the boat in the workspace the same as you’re playerID? That way every boat would have an unique name and you always know what it’s called
For that I made it so a spawned boat has the player’s name on the front of the name. But the boat has a unique name as well which is why I can’t find a way to remove only the previous boat if its spawned.
Here’s a screenshot:
Name the boat using ONLY the username, don’t have the “ShipTest2”, that way you can always reference the boat using the player.Name
Won’t that destroy the player’s character as well? O WAIT I can just put the boats inside a special folder makes sense now. Thanks I’ma try that now and inform you if its all good.