Dismounting Skateboard through GUI

  1. What do you want to achieve? Keep it simple and clear!
    – Working on a Skateboard Spawner that works through a GUI button, where if you click the button, you spawn a skateboard that you can ride around, but at the moment the only way you can get off the skateboard is through pressing Backspace, so I’m trying to make it so you can destroy the board through the GUI as well.
  2. What is the issue? Include screenshots / videos if possible!
    –I’m not sure how to implement the method of dismounting the board or just destroying an existing board when spawned and/or mounted. Look at code below I also tried setting up 2 separate remote events, but not sure how to use both of them in the local and server script.
  3. What solutions have you tried so far?
    Did you look for solutions on the Developer Hub?
    – I tried searching around, and found a few things that seemed like it was useful, but it didn’t seem to be applicable with what I was trying to achieve with the skateboard spawner. I am also very new to LUA, I wrote this when I’ve only been learning LUA for roughly a week or two.
-- This is the call script
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SkateboardEvent = ReplicatedStorage:WaitForChild("Skateboard")
local DestroySkateboardEvent = ReplicatedStorage:WaitForChild(("DestroySkateboard")
local Skateboard = script.Parent.Name
local SkateboardSpawner = script.Parent
local cooldown = 5
local pressed = false

script.Parent.MouseButton1Click:Connect(function()
	if (not pressed) then
		pressed = true
		SkateboardSpawner.ImageTransparency = 0
		SkateboardEvent:FireServer('Skateboard')
		wait(cooldown)
		pressed = false
		end
end)

-- DestroySkateboardEvent:FireServer('DestroySkateboard")
-- I don't know how to integrate this into the call script for when the button is pressed.
-- I also need to make a function for the DSB Event so that it destroys the board when its been spawned or is currently being used.

-- This is my existing server script for the SkateboardSpawner
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SkateboardEvent = game.ReplicatedStorage:WaitForChild("Skateboard")
local DestroySkateboardEvent = game.ReplicatedStorage:WaitForChild("DestroySkateboard")

local db = false

SkateboardEvent.OnServerEvent:Connect(function(player, Skateboard)

	if game.Workspace:FindFirstChild(player.Name.."'s Skateboard") then
		game.Workspace[player.Name.."'s Skateboard"]:Destroy() 
	end 
	if player.Character:FindFirstChild(player.Name.."'s Skateboard") then
		game.Workspace[player.Name.."'s Skateboard"]:Destroy()
	end
	local Skateboard = ServerStorage:FindFirstChild("Skateboard")
	if Skateboard then
		local clonedSkateboard = Skateboard:Clone()
		clonedSkateboard:MakeJoints()
		clonedSkateboard.Name = player.Name.."'s Skateboard"
		clonedSkateboard.Parent = game.Workspace
		clonedSkateboard:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,5))
		end
end)


DestroySkateboardEvent.OnServerEvent:Connect(function(player, Skateboard)
	if game.Workspace:FindFirstChild(player.Name.."'s Skateboard") then
		game.Workspace[player.Name.."'s Skateboard"]:Destroy() 
	end 
	if player.Character:FindFirstChild(player.Name.."'s Skateboard") then
		game.Workspace[player.Name.."'s Skateboard"]:Destroy()
	end
end)

If I understood correctly, you only use one button. I believe you could just use one remote event. Instead of spawning a new skateboard every time its’s pressed, just check whether a skateboard exists (which you are already doing), and if one exists, destroy it and don’t spawn a new one. If one doesn’t exist, spawn a new one.

This would mean that by clicking once the player could destroy their current skateboard. By clicking another time, they could spawn a new one if the spawncooldown has ended.

Client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local SkateboardEvent = ReplicatedStorage:WaitForChild("Skateboard")
local DestroySkateboardEvent = ReplicatedStorage:WaitForChild(("DestroySkateboard")
local Skateboard = script.Parent.Name
local SkateboardSpawner = script.Parent
local cooldown = 5
local spawnCooldDown = false
local plr = Players.LocalPlayer
local skateBoardName = plr.Name.."'s Skateboard"

script.Parent.MouseButton1Click:Connect(function()
	local currentSkateboard = workspace:FindFirstChild(skateboardName) or plr.Character:FindFirstChild(skateBoardName)
	if currentSkateboard or spawnCooldown == false then
		SkateboardSpawner.ImageTransparency = 0
		SkateboardEvent:FireServer('Skateboard')
		if not currentSkateboard and spawnCoolDown = false then
			spawnCoolDown = true
			wait(cooldown)
			spawnCoolDown = false
		end
	end
end)

Server:

SkateboardEvent.OnServerEvent:Connect(function(plr)
	local skateBoardName = plr.Name.."'s Skateboard"
	local currentSkateboard = workspace:FindFirstChild(skateboardName) or plr.Character:FindFirstChild(skateboardName)
	if currentSkateboard then
		currentSkateboard:Destroy()
	else
		local clonedSkateBoard = ServerStorage:FindFirstChild("Skateboard"):Clone()
		clonedSkateboard:MakeJoints()
		clonedSkateboard.Name = plr.Name.."'s Skateboard"
		clonedSkateboard:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,5))
		cloneSkateBoard.Parent = workspace
	end
end)
2 Likes

So, your initial code didn’t work when I tried running it as is after making a few corrections, but it did serve as a template for me to figure everything now. I have it working perfectly now. c:
Thank you so much.

1 Like
SkateboardEvent.OnServerEvent:Connect(function(player, Skateboard)
	local skateboardName = player.Name.."'s Skateboard"
	local spawnedBoard = game.Workspace:FindFirstChild(player.Name.."'s Skateboard")
	local currentSkateboard = player.Character:FindFirstChild(player.Name.."'s Skateboard")
	
	if spawnedBoard then -- Check to see if skateboard is spawned in game if so, then destroy
		spawnedBoard:Destroy()
	elseif currentSkateboard then -- check to see if skateboard is mounted, if so, then dismount
		currentSkateboard.SkateboardPlatform.PlatformMotor6D:Destroy()
		currentSkateboard.Parent.Humanoid.PlatformStand = false
		currentSkateboard:Destroy()
	else
		local clonedSkateboard = ServerStorage:FindFirstChild("Skateboard"):Clone() -- if no skateboard is spawned, spawn one
		clonedSkateboard:MakeJoints()
		clonedSkateboard.Name = player.Name.."'s Skateboard"
		clonedSkateboard.Parent = game.Workspace
		clonedSkateboard:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,5))
		end
end)

LOCAL SCRIPT:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local SkateboardEvent = ReplicatedStorage:WaitForChild("Skateboard")
local Skateboard = script.Parent.Name
local SkateboardSpawner = script.Parent
local cooldown = 5
local spawncooldown = false
local skateboardName = Players.Name.."'s Skateboard"
local plr = Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	local currentSkateboard = game.Workspace:FindFirstChild(skateboardName) or plr.Character:FindFirstChild(skateboardName)
	if currentSkateboard or spawncooldown == false then
		SkateboardSpawner.ImageTransparency = 0
		SkateboardEvent:FireServer('Skateboard')
		if not currentSkateboard and spawncooldown == false then
			spawncooldown = true
			wait(cooldown)
			spawncooldown = false
		end
	end
end)

The solution for dismounting while mounted without breaking anything, I just went into the skateboard script and saw what was going on when you actually dismount and pasted it in this script.