Help with scripting for game

So I’m making a Snow Fort Battle game and my problem is when anybody spawns in a snow fort it gets set to that snow fort to edit. Kinda hard to explain so here’s a video:

When you buy a fort you’re supposed to be able to edit where you want to place your own fort by pressing ‘F’. But when anybody in the server places a fort, everybody elses edit thing gets set to that fort. Also I need to figure out a way to delete your previous fort. Sorry for maybe sounding dumb… I’m new to scripting.

Scripts:

Server Script:

local rs = game:GetService("ReplicatedStorage")
za = rs:WaitForChild("zadd")
zm = rs:WaitForChild("zminus")
xa = rs:WaitForChild("xadd")
xm = rs:WaitForChild("xminus")
ya = rs:WaitForChild("yadd")
ym = rs:WaitForChild("yminus")
r = rs:WaitForChild("rotate")
local fs = rs:WaitForChild("fstart")
fe = rs:WaitForChild("fend")
uiplr1 = rs:WaitForChild("uigotoplayer")
uiplr2 = rs:WaitForChild("uigotoplr2")
uiplr3 = rs:WaitForChild("uigotoplr3")
uiplr4 = rs:WaitForChild("uigotoplr4")
uiplr5 = rs:WaitForChild("uigotoplr5")
uiplr6 = rs:WaitForChild("uigotoplr6")


_G.fort = game.ReplicatedStorage.Fort3
fortxadd = Vector3.new(5, 0, 0)
fortxminus = Vector3.new(-5, 0, 0)
fortzadd = Vector3.new(0, 0, 5)
fortzminus = Vector3.new(0, 0, -5)
fortyadd = Vector3.new(0, 1, 0)
fortyminus = Vector3.new(0, -1, 0)


xa.OnServerEvent:Connect(function(player)
	_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortxadd) -- z pressed
end)

xm.OnServerEvent:Connect(function(player)
	_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortxminus) -- x pressed
end)

za.OnServerEvent:Connect(function(player)
_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortzadd) -- c pressed
end)

zm.OnServerEvent:Connect(function(player)
_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortzminus) -- v pressed
end)

ym.OnServerEvent:Connect(function(player)
_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortyminus) -- b pressed
end)

ya.OnServerEvent:Connect(function(player)
_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame() + fortyadd) -- n pressed
end)

r.OnServerEvent:Connect(function(player)
_G.fort:SetPrimaryPartCFrame(_G.fort:GetPrimaryPartCFrame()*CFrame.Angles(math.rad(0),-0.785398,0)) -- r pressed
end)

fs.OnServerEvent:Connect(function(player)
for i,v in pairs(_G.fort:GetChildren()) do v.Transparency = 0.6 end   --f key pressed to start edit
for i,v in pairs(_G.fort:GetChildren()) do v.CanCollide = false end
end)

fe.OnServerEvent:Connect(function(player)
for i,v in pairs(_G.fort:GetChildren()) do v.Transparency = 0 end
for i,v in pairs(_G.fort:GetChildren()) do v.CanCollide = true end  --f key pressed again to end edit
end)

uiplr1.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort1:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))  -- When the shop GUI is pressed it sets 'fort' to that fort and puts it in front of you. vvv
end)

uiplr2.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort2:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))
end)

uiplr3.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort3:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))
end)

uiplr4.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort4:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))
end)

uiplr5.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort5:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))
end)

uiplr6.OnServerEvent:connect(function(player)
	_G.fort = game.ReplicatedStorage.Fort6:Clone()
	_G.fort.Parent = game.Workspace
	_G.fort:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, -3, 3))
end)

One of the GUI button’s scripts:

local bought = false
local reps = game:GetService("ReplicatedStorage")
plrgoto = reps:WaitForChild("uigotoplayer")

script.Parent.MouseButton1Click:Connect(function()
	if bought == false then
		plrgoto:FireServer()
		bought = true
	end
end)

Script inside StarterPlayerScripts for moving the fort:

local userinputservice = game:GetService("UserInputService")
local rs = game:GetService("ReplicatedStorage")
za = rs:WaitForChild("zadd")
zm = rs:WaitForChild("zminus")
xa = rs:WaitForChild("xadd")
xm = rs:WaitForChild("xminus")
ya = rs:WaitForChild("yadd")
ym = rs:WaitForChild("yminus")
r = rs:WaitForChild("rotate")
fs = rs:WaitForChild("fstart")
fe = rs:WaitForChild("fend")

fortxadd = Vector3.new(5, 0, 0)
fortxminus = Vector3.new(-5, 0, 0)
fortzadd = Vector3.new(0, 0, 5)
fortzminus = Vector3.new(0, 0, -5)
fortyadd = Vector3.new(0, 1, 0)
fortyminus = Vector3.new(0, -1, 0)
placed = true

userinputservice.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.X then
		if placed == false then
			xa:FireServer()
			end
	end
	
	if input.KeyCode == Enum.KeyCode.Z then
		if placed == false then
			xm:FireServer()
			end
	end
	
	if input.KeyCode == Enum.KeyCode.C then
		if placed == false then
			za:FireServer()
			end
	end
	
	if input.KeyCode == Enum.KeyCode.V then
		if placed == false then
			zm:FireServer()
			end
	end
	
	if input.KeyCode == Enum.KeyCode.B then
		if placed == false then
			ym:FireServer()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.N then
		if placed == false then
			ya:FireServer()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.R then
		if placed == false then
			r:FireServer()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.F then
		if placed == true then
			placed = false
			fs:FireServer()
			return
		end
		if placed == false then
			placed = true
			fe:FireServer()
			return
		end
	end
end)
1 Like

yeah sorry I was never planning on having people look at my code so everything has shortened names

It’s pretty clear why this is happening, you seem to use _G.fort for all players. You may want to store the player’s fort underneath a individual key in the _G table (ie. userid-fort).

For example:

local key = player.UserId .. "-fort";
_G[key] = ...

I wouldn’t necessarily recommend using _G, it is not best practice as opposed to using BindableEvents or a class. However, if your game revolves around it already then there’s no point in swapping over.

Wow I just spent 40 minutes trying to understand that and how I would use that in my game and now my brain is jelly.

In the GUI script would I do
_G.localPlr = game.Players.LocalPlayer
and then try do something with that in the server script? Because I couldn’t think of a way to set the fort to your local player by using that.

1 Like