Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I change the position of var1 to be the position of var2?

So I'm creating a spawn system for weapons, what it does is go through a table of spawns selects one then goes through a table of weapons and selects one. Prints me the variable for where it is supposed to spawn then clones the weapon from ReplicatedStorage to the SpawnPoint chosen

I have it working and spawning the weapons but I can't get the weapons position set to the spawn point.

This is my code as I have it:

spawnpoints = game.Workspace:FindFirstChild("Spawn Point")
sp1=spawnpoints.sp1
sp2=spawnpoints.sp2
sp3=spawnpoints.sp3
sp4=spawnpoints.sp4
sp5=spawnpoints.sp5
sp6=spawnpoints.sp6

pb=game.ReplicatedStorage.pb_gun
sword=game.ReplicatedStorage.sword
timebomb=game.ReplicatedStorage.timebomb
rocket=game.ReplicatedStorage.rocket


function spawn_weapon()
	local debounce = false
		local spawns={sp1,sp2,sp3,sp4,sp5,sp6}
		local spawnpoint=spawns[math.random(1, #spawns)]
		local spawnpos=spawnpoint.Position
		print(spawnpoint)
		local weapons={pb,sword,timebomb,rocket}
		local weapon=weapons[math.random(1, #weapons)]
		local weaponpos=weapon.Handle.Position
		if debounce == false then
			weapon:Clone()
			weapon.Parent=spawnpoint
			weaponpos=CFrame.new(spawnpos)
			print ("Weapon Spawned")
			debounce = true
			wait(3)
			debounce = false
		end
		
		
end

while true do
	wait(1)
	spawn_weapon()
end

Everything here is working except the position and I'm getting no errors in output yet the weapon still spawns in the area I place it before I put it in replicatedstorage.

1 answer

✓ Accepted answer

By changing the weaponpos variable, you are simply changing the variable.

The syntax for definition involves indexing/referencing the desired value..

Also, you aren't cloning the weapon correctly. You have to reference your clone and set it's Parent Property..

Numerous other variable redundancies fixed..

local rs = game.ReplicatedStorage --remember to use variables
local spawns = {
	spawnpoints.sp1;
	spawnpoints.sp2;
	spawnpoints.sp3;
	spawnpoints.sp4;
	spawnpoints.sp5;
	spawnpoints.sp6;
}
local weapons = {
	rs.pb_gun;
	rs.sword;
	rs.timebomb;
	rs.rocket;
}
local debounce = false --Define debounce on the outside..


function spawn_weapon()
	if not debounce then --check debounce..
		debounce = true --enable debounce
		local spawnpoint = spawns[math.random(#spawns)] --1 is defaulted
		local weapon = weapons[math.random(#weapons)]
		print(spawnpoint,weapon)
		local c = weapon:Clone() --reference the clone
		c.Position = spawnpoint.Position --definition syntax
		c.Parent = spawnpoint
		print ("Weapon Spawned")
		wait(3)
		debounce = false --disable debounce
	end
end

while wait(1) do --You can put the `wait` function here ;D
	spawn_weapon()
end

2 comments

1

This worked though I needed to make one teeny edit changing c.Position to c.Handle.Position because a tool has no 'position' variable.

1

Glad to hear you were able to debug with ease :D Happy developing mate.

This discussion is preserved as it was. New answers, comments, and votes are closed.