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

A bug that is annoying me a lot! Can someone help me?

What I'm having a problem with is that I made a hammer that allows me to build fences, and so far I'm very happy with the progress! However when I tried it on a local server, it just refuses to work, even printing a string of words, you know, print("Something like this!"). I tried connecting it to the server with a RemoteEvent but it still doesn't work, only on the server! I have no idea why! I might just be blind and stupid but oh well.

I just can't seem to spot the fix. I'm also not sure if it's because of the tool itself or the scripts contained inside because I copied the exact tool but changed the script to just place a brick where you click in the workspace, it works like I expected it to without flaws. Be warned that the script you're about to see is long. 202 lines as of now.

THIS IS CLIENT SIDED!

local plr = game.Players.LocalPlayer
local tool = script.Parent

local mouse = plr:GetMouse()

local canSelectOption = true
local canPlaceItem = false
local isHoldingShift = false
local isShowingCantPlace = false
local isSetToGreen = true
local toolIsEquipped = false

local previousAngle = 0

local originalPartColor = {}
local originalPartTransparency = {}
local originalPartMaterial = {}

repeat wait() until plr.PlayerGui.HammerBuildingGui ~= nil
repeat wait() until game.ServerStorage.Fences ~= nil

script.Parent.Print:FireServer("Loaded Hammer Script")

local function resetAllValues()
	canSelectOption = true
	canPlaceItem = false
	isHoldingShift = false
	isShowingCantPlace = false
	isSetToGreen = true
	
	previousAngle = 0
	
	originalPartColor = {}
	originalPartTransparency = {}
	originalPartMaterial = {}
end

local function changeFenceToRed()
	pcall(function()
		script.Parent.Print:FireServer("Changed to red")
		
		for i = 1, #fenceSelected:GetChildren() do
			local parts = fenceSelected:GetChildren()
			
			if parts[i]:IsA("BasePart") and parts[i].Name ~= "FenceCollision" then
				parts[i].Transparency = 0.5
				parts[i].Material = "SmoothPlastic"
				parts[i].BrickColor = BrickColor.new(Color3.fromRGB(255,100,100))
			end
		end
	end)
end

local function changeFenceToGreen()
	pcall(function()
		script.Parent.Print:FireServer("Changed to green")
		
		for i = 1, #fenceSelected:GetChildren() do
			local parts = fenceSelected:GetChildren()
			
			if parts[i]:IsA("BasePart") and parts[i].Name ~= "FenceCollision" then
				parts[i].Transparency = 0.5
				parts[i].Material = "SmoothPlastic"
				parts[i].BrickColor = BrickColor.new(Color3.fromRGB(50,255,50))
			end
		end
	end)
end

for i, option in pairs(plr.PlayerGui.HammerBuildingGui.Frame.OptionsScrollingFrame:GetChildren()) do
	if option:IsA("TextButton") and game.ServerStorage.Fences:FindFirstChild(option.Name) then
		option.MouseButton1Click:connect(function()
			script.Parent.Print:FireServer("Clicked option: " .. option.Name)
			
			script.Parent.Build:FireServer(option, canSelectOption)
			
			if canSelectOption == true and toolIsEquipped == true then
				local function createFence()
					canSelectOption = false
					
					fenceSelected = game.ServerStorage.Fences[option.Name]:Clone()
					fenceSelected.FenceCollision.CanCollide = false
					fenceSelected.Parent = workspace
					
					for i = 1, #fenceSelected:GetChildren() do
						local parts = fenceSelected:GetChildren()
						
						if parts[i]:IsA("BasePart") and parts[i].Name ~= "FenceCollision" then
							originalPartColor[i] = parts[i].BrickColor
							originalPartTransparency[i] = parts[i].Transparency
							originalPartMaterial[i] = parts[i].Material
						end
					end
					
					changeFenceToGreen()
					
					if isHoldingShift == true then
						fenceSelected:SetPrimaryPartCFrame(fenceSelected:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(previousAngle),0))
					end
					
					mouse.TargetFilter = fenceSelected
					
					repeat
						fenceSelected:TranslateBy(Vector3.new(mouse.Hit.p.X,((fenceSelected.Settings.FenceHeight.Value / 2) - 0.2) + mouse.Hit.p.Y, mouse.Hit.p.Z) - fenceSelected:GetPrimaryPartCFrame().p)
						
						if mouse.Target and isShowingCantPlace == false then							
							if mouse.Target.Name == "PlaceableVisibility" then
								canPlaceItem = true
								
								if isSetToGreen == false then
									isSetToGreen = true
									
									changeFenceToGreen()
								end
							else
								canPlaceItem = false
								
								if isSetToGreen == true then
									isSetToGreen = false
									
									changeFenceToRed()
								end
							end
						end
						
						game:GetService("RunService").RenderStepped:Wait()
					until canSelectOption == true
					
					if isHoldingShift == true then
						createFence()
					end
				end
				
				createFence()
			end
		end)
	end
end

tool.Equipped:connect(function()
	script.Parent.Print:FireServer("Equipped tool")
	toolIsEquipped = true
	
	resetAllValues()
	
	pcall(function()
		plr.PlayerGui.HammerBuildingGui.Frame:TweenPosition(UDim2.new(0,5,1,-255), "Out", "Quad", 0.5)
	end)
end)

tool.Unequipped:connect(function()
	script.Parent.Print:FireServer("Unequipped tool")
	toolIsEquipped = false
	
	resetAllValues()
	fenceSelected:Destroy()
	
	pcall(function()
		plr.PlayerGui.HammerBuildingGui.Frame:TweenPosition(UDim2.new(0,-300,1,-255), "Out", "Quad", 0.5)
	end)
end)

mouse.Button1Down:connect(function()
	script.Parent.Print:FireServer("Button1Down")
	
	if canSelectOption == false and canPlaceItem == true and plr.leaderstats.Cash.Value >= fenceSelected.Settings.Price.Value and plr.leaderstats.Level.Value >= fenceSelected.Settings.LevelRequired.Value then
		canSelectOption = true
		
		script.Parent.UpdateStats:FireServer(plr.leaderstats, fenceSelected.Settings.Price.Value, fenceSelected.Settings.LevelRequired.Value)
		script.Parent.Build:FireServer(fenceSelected, originalPartColor, originalPartTransparency, originalPartMaterial)
	elseif canSelectOption == false and canPlaceItem == true then
		isShowingCantPlace = true
		changeFenceToRed()
		wait(0.1)
		changeFenceToGreen()
		isShowingCantPlace = false
	end
end)

game:GetService("UserInputService").InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.Q and canSelectOption == false then
		canSelectOption = true
		canPlaceItem = false
		
		originalPartColor = {}
		originalPartTransparency = {}
		originalPartMaterial = {}
		
		fenceSelected:Destroy()
	elseif key.KeyCode == Enum.KeyCode.R and canSelectOption == false and canPlaceItem == true then
		fenceSelected:SetPrimaryPartCFrame(fenceSelected:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(45),0))
		previousAngle = previousAngle + 45
	elseif key.KeyCode == Enum.KeyCode.LeftShift then
		isHoldingShift = true
	end
end)

game:GetService("UserInputService").InputEnded:connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		isHoldingShift = false
	end
end)

ATTENTION! Okay, now I've got your attention, I'm just telling you that the next script is server sided.

script.Parent.Build.OnServerEvent:connect(function(plr, fenceSelected, originalPartColor, originalPartTransparency, originalPartMaterial)
	for i = 1, #fenceSelected:GetChildren() do
		local parts = fenceSelected:GetChildren()
		
		if parts[i]:IsA("BasePart") and parts[i].Name ~= "FenceCollision" then
			parts[i].BrickColor = originalPartColor[i]
			parts[i].Transparency = originalPartTransparency[i]
			parts[i].Material = originalPartMaterial[i]
		elseif parts[i]:IsA("BasePart") and parts[i].Name == "FenceCollision" then
			parts[i].CanCollide = true
		end
	end
end)

script.Parent.UpdateStats.OnServerEvent:connect(function(plr, stats, price, level)
	stats.Cash.Value = stats.Cash.Value - price
end)

script.Parent.Print.OnServerEvent:connect(function(plr, message)
	print(message)
end)

2 comments

0

Any errors?

0

Nope, no errors at all. Server and client doesn’t show any errors.

1 answer

Sorry guys, I fixed it. It's because I was trying to access the ServerStorage with a local script.

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