Campfire Spawnpoint

  1. I would like to achieve a script that sets your Spawnpoint at a X, Y, Z coordinate when a part is clicked.

  2. So far, I’ve looked on the Devforum and found nothing (please note I am not a coder) other than the Roblox Mouse Click Detect. I’m wondering if I would be able to use the script in the default spawnpoint, if there even is one.

Thank you for reading this, and any help is appreciated. Have a good day!

2 Likes

I’m not sure if you’re familiar with AllowTeamChangeOnTouch

AllowTeamChangeOnTouch

SpawnLocation | Roblox Creator Documentation

1 Like

Feel like this previous DevForum answer can help you out quite a bit; look into the code and familiarize yourself with the Services being utilized to gain a better understanding though.

nope, thanks! sorry, i didn’t see that

i mean when you click a part, not touching it, unless those are different?

It is different but the process is similar, as you will need a ClickDetector on the part, but all you have to do is edit the code to have it call that function to set the spawnPoint - I’ll go ahead and use Forummer’s solution below the linked DevForum post:

local players = game:GetService("Players")
local spawnFolder = workspace.Folder --Path to folder of spawn parts.

local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end
		
		if player.Spawn.Value then
			task.wait()
			character:PivotTo(player.Spawn.Value:GetPivot())
		end
	end
	
	player.CharacterAdded:Connect(onCharacterAdded)
	
	local _spawn = Instance.new("ObjectValue")
	_spawn.Name = "Spawn"
	_spawn.Parent = player
end

players.PlayerAdded:Connect(onPlayerAdded)

local function onSpawnTouched(Player, _spawn)
	if Player:FindFirstChild("Character)" then
		hitPlayer.Spawn.Value = _spawn
	end
end

for _, _spawn in ipairs(spawnFolder:GetChildren()) do
	if _spawn:IsA("BasePart") then
		_spawn:FindFirstChild("ClickDetector").MouseClick:Connect(function(Player)
			onSpawnTouched(Player, _spawn)
		end)
	end
end

At least, something like that.

I’ve put it in a script inside the ClickDetector, seems that it doesn’t work. I don’t know if I’m supposed to edit it or something, sorry!

CustomSpawn.rbxl (42.6 KB)

image

image

local SpawnListPerPlayer = {}

local function onPlayerAdded(player)	
	local function onCharacterAdded(character)
		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end
		if SpawnListPerPlayer[player.UserId]~= nil then
			task.wait()
			character:PivotTo(SpawnListPerPlayer[player.UserId]:GetPivot())
		end
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

local function onPlayerRemoving(player)
	SpawnListPerPlayer[player.UserId] = nil
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)

local SpawnDir = workspace:WaitForChild("Spawns")

local function onSpawnClicked(player, s)
	if s:IsA("Model") then
		s = s.PrimaryPart
	end
	if s then
		SpawnListPerPlayer[player.UserId] = s
	end
end

for _, s in ipairs(SpawnDir:GetChildren()) do
	if s:IsA("Model") or s:IsA("BasePart") then
		s:WaitForChild("ClickDetector").MouseClick:Connect(function(player)
			onSpawnClicked(player, s)
		end)
	end
end

image

local function onSpawnClicked(player, s)
	if s:IsA("Model") then
		s = s.PrimaryPart
	end
	if s then
		for _,i in pairs(script.Parent:GetChildren()) do
			if i:IsA("Model") or i:IsA("BasePart") and i ~= s then
				i.Color = i:GetAttribute("Color")
			end
		end
		s.Color = Color3.new(1,0,0)
	end
end

for _, s in ipairs(script.Parent:GetChildren()) do
	if s:IsA("Model") or s:IsA("BasePart") then
		if not s:GetAttribute("Color") then
			s:SetAttribute("Color",s.Color)
		end
		s:WaitForChild("ClickDetector").MouseClick:Connect(function(player)
			onSpawnClicked(player, s)
		end)
	end
end

Hope this helps you out.

this works perfectly, but again I’m not a coder, and is there any way for there to be an offset to spawn at?

Sure, let me modify this and give you an example.

alright, thank you once again!

1 Like

CustomCampfireSpawn.rbxl (162.2 KB)
image

Ok… I made some changes…
The ‘spawns’ must be a ‘Model’ and the part that you click on, is the model’s ‘PrimaryPart’

Also, the code that places the character to its spawn location has been modified to place the character at a random position around the spawn (campfire)

		--If the player's character respawns, we do this...
		if SpawnListPerPlayer[player.UserId]~= nil then
			task.wait()
			--we get the campfire's cframe
			local cf = SpawnListPerPlayer[player.UserId].PrimaryPart:GetPivot()
			--we rotate the cframe by 45 degrees, multiplied by a random number between 0-7
			cf = cf * CFrame.Angles(0,math.rad(math.random(0,7)*45),0)
			--we create a new cframe, by stepping forward from the campfire's rotated cframe,
			--then looking back from that position to the campfire
			local spawnCFrame = CFrame.lookAt(cf.Position+(cf.lookVector*5),cf.Position)
			--this new cframe is used to spawn the character
			character:PivotTo(spawnCFrame)
		end

All the other scripts have been adjusted for this…
image

local SpawnListPerPlayer = {} --This is a list of players that have a spawn location selected

local function onPlayerAdded(player)	
	local function onCharacterAdded(character)
		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end
		--If the player's character respawns, we do this...
		if SpawnListPerPlayer[player.UserId]~= nil then
			task.wait()
			--we get the campfire's cframe
			local cf = SpawnListPerPlayer[player.UserId].PrimaryPart:GetPivot()
			--we rotate the cframe by 45 degrees, multiplied by a random number between 0-7
			cf = cf * CFrame.Angles(0,math.rad(math.random(0,7)*45),0)
			--we create a new cframe, by stepping forward from the campfire's rotated cframe,
			--then looking back from that position to the campfire
			local spawnCFrame = CFrame.lookAt(cf.Position+(cf.lookVector*5),cf.Position)
			--this new cframe is used to spawn the character
			character:PivotTo(spawnCFrame)
		end
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

local function onPlayerRemoving(player)
	SpawnListPerPlayer[player.UserId] = nil
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)

local SpawnDir = workspace:WaitForChild("Spawns")

for _, s in ipairs(SpawnDir:GetChildren()) do
	if s:IsA("Model") then
		s.PrimaryPart:WaitForChild("ClickDetector").MouseClick:Connect(function(player)
			SpawnListPerPlayer[player.UserId] = s
		end)
	end
end

image

--this script just detects when to Enable or Disable the fire effects of each campfire
--by setting the Activated custom attribute for each campfire

local campList = {} --will hold the list of campfires

for _,i in ipairs(script.Parent:GetChildren()) do --loop through all the campfires
	if i:IsA("Model") then
		i:SetAttribute("Activated",false) --set each campfires Activated to false (this is default)
		table.insert(campList,i) --put a reference of this campfire into the list
		i.PrimaryPart:WaitForChild("ClickDetector").MouseClick:Connect(function(player) --check for a mouse click on the campfire
			--if the campfire was clicked...
			i:SetAttribute("Activated",true) --set the campfire's Activated to true
			--loop through all the campfires, and if it is NOT the one we just clicked, turn it off
			for _,z in pairs(campList) do
				if z~= i then
					z:SetAttribute("Activated",false) --set campfire's Activated to false
				end
			end
		end)
	end
end

--this script basicallly just waits to see if the campfire's custom attribute 'Activated' changes
--if it does, this checks to see if its true or false
local campfire = script.Parent
local light = campfire:WaitForChild("Fire"):WaitForChild("PointLight")
local flame = campfire:WaitForChild("Fire"):WaitForChild("Flame")
local icon = campfire:WaitForChild("Icon")
local smoke = campfire:WaitForChild("Fire"):WaitForChild("Smoke")

campfire:GetAttributeChangedSignal("Activated"):Connect(function()
	--if Activated is true, turn on the effects
	if campfire:GetAttribute("Activated") == true then
		light.Enabled = true
		flame.Enabled = true
		smoke.Enabled = true
		icon.Parent = nil
	else --if Activated is false, turn off the effects
		light.Enabled = false
		flame.Enabled = false
		smoke.Enabled = false
		icon.Parent = campfire
	end
end)

Don’t take this the wrong way, but I feel you either need to do one of two things.

Either take some time to really get into learning how to script.
Or partner up with a scripter to help you work on your project.

Its good you ask for help in the forums as you did, but the chance of someone responding with fully written scripts and place files is very rare.

Though I love to help, its rare for me to write up a detailed example like this as my free time is pretty limited.

I tried to make it easy to understand, though so maybe you can get a better idea of what is happening in the code.

Hope this helps you out.

2 Likes

I understand about having to learn how to script, and thank you for the suggestion again. I hope to learn Lua sometime soon, and thank you once again for this. Please have a good day!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.