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!
001 | local plr = game.Players.LocalPlayer |
002 | local tool = script.Parent |
004 | local mouse = plr:GetMouse() |
006 | local canSelectOption = true |
007 | local canPlaceItem = false |
008 | local isHoldingShift = false |
009 | local isShowingCantPlace = false |
010 | local isSetToGreen = true |
011 | local toolIsEquipped = false |
013 | local previousAngle = 0 |
015 | local originalPartColor = { } |
016 | local originalPartTransparency = { } |
017 | local originalPartMaterial = { } |
019 | repeat wait() until plr.PlayerGui.HammerBuildingGui ~ = nil |
020 | repeat wait() until game.ServerStorage.Fences ~ = nil |
022 | script.Parent.Print:FireServer( "Loaded Hammer Script" ) |
024 | local function resetAllValues() |
025 | canSelectOption = true |
027 | isHoldingShift = false |
028 | isShowingCantPlace = false |
033 | originalPartColor = { } |
034 | originalPartTransparency = { } |
035 | originalPartMaterial = { } |
038 | local function changeFenceToRed() |
040 | script.Parent.Print:FireServer( "Changed to red" ) |
042 | for i = 1 , #fenceSelected:GetChildren() do |
043 | local parts = fenceSelected:GetChildren() |
045 | if parts [ i ] :IsA( "BasePart" ) and parts [ i ] .Name ~ = "FenceCollision" then |
046 | parts [ i ] .Transparency = 0.5 |
047 | parts [ i ] .Material = "SmoothPlastic" |
048 | parts [ i ] .BrickColor = BrickColor.new(Color 3. fromRGB( 255 , 100 , 100 )) |
054 | local function changeFenceToGreen() |
056 | script.Parent.Print:FireServer( "Changed to green" ) |
058 | for i = 1 , #fenceSelected:GetChildren() do |
059 | local parts = fenceSelected:GetChildren() |
061 | if parts [ i ] :IsA( "BasePart" ) and parts [ i ] .Name ~ = "FenceCollision" then |
062 | parts [ i ] .Transparency = 0.5 |
063 | parts [ i ] .Material = "SmoothPlastic" |
064 | parts [ i ] .BrickColor = BrickColor.new(Color 3. fromRGB( 50 , 255 , 50 )) |
070 | for i, option in pairs (plr.PlayerGui.HammerBuildingGui.Frame.OptionsScrollingFrame:GetChildren()) do |
071 | if option:IsA( "TextButton" ) and game.ServerStorage.Fences:FindFirstChild(option.Name) then |
072 | option.MouseButton 1 Click:connect( function () |
073 | script.Parent.Print:FireServer( "Clicked option: " .. option.Name) |
075 | script.Parent.Build:FireServer(option, canSelectOption) |
077 | if canSelectOption = = true and toolIsEquipped = = true then |
078 | local function createFence() |
079 | canSelectOption = false |
081 | fenceSelected = game.ServerStorage.Fences [ option.Name ] :Clone() |
082 | fenceSelected.FenceCollision.CanCollide = false |
083 | fenceSelected.Parent = workspace |
085 | for i = 1 , #fenceSelected:GetChildren() do |
086 | local parts = fenceSelected:GetChildren() |
088 | if parts [ i ] :IsA( "BasePart" ) and parts [ i ] .Name ~ = "FenceCollision" then |
089 | originalPartColor [ i ] = parts [ i ] .BrickColor |
090 | originalPartTransparency [ i ] = parts [ i ] .Transparency |
091 | originalPartMaterial [ i ] = parts [ i ] .Material |
097 | if isHoldingShift = = true then |
098 | fenceSelected:SetPrimaryPartCFrame(fenceSelected:GetPrimaryPartCFrame() * CFrame.Angles( 0 ,math.rad(previousAngle), 0 )) |
101 | mouse.TargetFilter = fenceSelected |
104 | fenceSelected:TranslateBy(Vector 3. new(mouse.Hit.p.X,((fenceSelected.Settings.FenceHeight.Value / 2 ) - 0.2 ) + mouse.Hit.p.Y, mouse.Hit.p.Z) - fenceSelected:GetPrimaryPartCFrame().p) |
106 | if mouse.Target and isShowingCantPlace = = false then |
107 | if mouse.Target.Name = = "PlaceableVisibility" then |
110 | if isSetToGreen = = false then |
118 | if isSetToGreen = = true then |
126 | game:GetService( "RunService" ).RenderStepped:Wait() |
127 | until canSelectOption = = true |
129 | if isHoldingShift = = true then |
140 | tool.Equipped:connect( function () |
141 | script.Parent.Print:FireServer( "Equipped tool" ) |
142 | toolIsEquipped = true |
147 | plr.PlayerGui.HammerBuildingGui.Frame:TweenPosition(UDim 2. new( 0 , 5 , 1 ,- 255 ), "Out" , "Quad" , 0.5 ) |
151 | tool.Unequipped:connect( function () |
152 | script.Parent.Print:FireServer( "Unequipped tool" ) |
153 | toolIsEquipped = false |
156 | fenceSelected:Destroy() |
159 | plr.PlayerGui.HammerBuildingGui.Frame:TweenPosition(UDim 2. new( 0 ,- 300 , 1 ,- 255 ), "Out" , "Quad" , 0.5 ) |
163 | mouse.Button 1 Down:connect( function () |
164 | script.Parent.Print:FireServer( "Button1Down" ) |
166 | 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 |
167 | canSelectOption = true |
169 | script.Parent.UpdateStats:FireServer(plr.leaderstats, fenceSelected.Settings.Price.Value, fenceSelected.Settings.LevelRequired.Value) |
170 | script.Parent.Build:FireServer(fenceSelected, originalPartColor, originalPartTransparency, originalPartMaterial) |
171 | elseif canSelectOption = = false and canPlaceItem = = true then |
172 | isShowingCantPlace = true |
176 | isShowingCantPlace = false |
180 | game:GetService( "UserInputService" ).InputBegan:connect( function (key) |
181 | if key.KeyCode = = Enum.KeyCode.Q and canSelectOption = = false then |
182 | canSelectOption = true |
185 | originalPartColor = { } |
186 | originalPartTransparency = { } |
187 | originalPartMaterial = { } |
189 | fenceSelected:Destroy() |
190 | elseif key.KeyCode = = Enum.KeyCode.R and canSelectOption = = false and canPlaceItem = = true then |
191 | fenceSelected:SetPrimaryPartCFrame(fenceSelected:GetPrimaryPartCFrame() * CFrame.Angles( 0 ,math.rad( 45 ), 0 )) |
192 | previousAngle = previousAngle + 45 |
193 | elseif key.KeyCode = = Enum.KeyCode.LeftShift then |
194 | isHoldingShift = true |
198 | game:GetService( "UserInputService" ).InputEnded:connect( function (key) |
199 | if key.KeyCode = = Enum.KeyCode.LeftShift then |
200 | isHoldingShift = false |
ATTENTION! Okay, now I've got your attention, I'm just telling you that the next script is server sided.
01 | script.Parent.Build.OnServerEvent:connect( function (plr, fenceSelected, originalPartColor, originalPartTransparency, originalPartMaterial) |
02 | for i = 1 , #fenceSelected:GetChildren() do |
03 | local parts = fenceSelected:GetChildren() |
05 | if parts [ i ] :IsA( "BasePart" ) and parts [ i ] .Name ~ = "FenceCollision" then |
06 | parts [ i ] .BrickColor = originalPartColor [ i ] |
07 | parts [ i ] .Transparency = originalPartTransparency [ i ] |
08 | parts [ i ] .Material = originalPartMaterial [ i ] |
09 | elseif parts [ i ] :IsA( "BasePart" ) and parts [ i ] .Name = = "FenceCollision" then |
10 | parts [ i ] .CanCollide = true |
15 | script.Parent.UpdateStats.OnServerEvent:connect( function (plr, stats, price, level) |
16 | stats.Cash.Value = stats.Cash.Value - price |
19 | script.Parent.Print.OnServerEvent:connect( function (plr, message) |