Local script in GUI won't run

You can write your topic however you want, but you need to answer these questions:

  1. Make the local script run, I think it is related to a module. Also all the syntaxes and destinations are considered to be correct

  2. What is the issue?
    Scripts:
    -The problem recurring script

local UserInput = game:GetService("UserInputService")
local Run = game:GetService("RunService")
local Physics = game:GetService("PhysicsService")

local Players = game:GetService("Players")
local LocalPlyr = Players.LocalPlayer
local Cash = LocalPlyr:WaitForChild("Cash")

local Gui = script.Parent
local Camera = workspace.CurrentCamera 

local Storage = game:GetService("ReplicatedStorage")
local Towers = Storage:WaitForChild("Towers")

local Events = Storage:WaitForChild("Events")
local SpawnEvent = Events:WaitForChild("SpawnTower")

local Functions = Storage:WaitForChild("Functions")
local RequestTwrFunction = Functions:WaitForChild("RequestTower")
local GetMapFunction = Functions:WaitForChild("GetMap")

local Modules = Storage:WaitForChild("Modules")
local HlthModule = require(Modules:WaitForChild("Health"))

local NewTower
local Rotation = 0
local PlacedTowers = 0
local PlcdTwrsHitbox
local MaxTowers = 60/#Players:GetPlayers()
local Map = GetMapFunction:InvokeServer()
local Info = workspace:WaitForChild("Info")

HlthModule.BaseSetup(1000)

Info.Message.Changed:Connect(function()
	Gui.Topbar.Right.Message.Text = Info.Message.Value
end)

local function UpdateCash()
	Gui.Topbar.Left.Cash.Text = Cash.Value.."$"
end

UpdateCash()
Cash.Changed:Connect(UpdateCash)

local function RemovePlaceHolder()
	if NewTower then
		NewTower:Destroy()
		NewTower = nil
		Rotation = 0

		for i,v in pairs(workspace.Towers:GetChildren()) do
			PlcdTwrsHitbox = v.Hitbox
			PlcdTwrsHitbox.Transparency = 1
		end
		PlcdTwrsHitbox = nil
	end
end

local function AddPlaceHolder(name)
	RemovePlaceHolder()	

	local Tower = Towers:FindFirstChild(name)
	if Tower then
		NewTower = Tower:Clone()
		NewTower.Parent = workspace.Towers

		for i,v in ipairs(NewTower:GetDescendants()) do
			if v:IsA("BasePart")then
				Physics:SetPartCollisionGroup(v,"Entity")
				v.Transparency = 0.5

			end
		end
	end
end

local function ColorPlaceHolder(color)
	for i,v in ipairs(NewTower:GetDescendants()) do
		if v:IsA("BasePart")then
			v.Color = color
		end
	end
	NewTower.Hitbox.Color = Color3.new(color.R,color.G,1)
end

Gui.Towers.Title.Text = "Towers: "..PlacedTowers.."/"..MaxTowers
for i,tower in pairs(Towers:GetChildren())do
	local button = Gui.Towers.Template:Clone()
	local config = tower:WaitForChild("Config")
	button.Name = tower.Name
	button.Image = config.Image.Texture
	button.Visible = true
	button.Parent = Gui.Towers
	button.Price.Text = config.Price.Value.."$" 
	button.LayoutOrder = i

	button.Activated:Connect(function()
		local CanPlace = RequestTwrFunction:InvokeServer(tower.Name)
		if CanPlace then
			AddPlaceHolder(tower.Name)		
		end
	end)
end

local function MouseRaycast(blacklist)
	local MousePos = UserInput:GetMouseLocation()
	local MouseRay = Camera:ViewportPointToRay(MousePos.X,MousePos.Y)
	local RycstParams = RaycastParams.new()

	RycstParams.FilterType = Enum.RaycastFilterType.Exclude
	RycstParams.FilterDescendantsInstances = blacklist

	local RycstResult = workspace:Raycast(MouseRay.Origin,MouseRay.Direction*1000,RycstParams)
	return RycstResult
end	

UserInput.InputBegan:Connect(function(input,processed)
	if processed then
		return
	elseif NewTower then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if CanPlace then
				SpawnEvent:FireServer(NewTower.Name,NewTower.PrimaryPart.CFrame)
				PlacedTowers += 1
				Gui.Towers.Title.Text = "Towers: "..PlacedTowers.."/"..MaxTowers
				RemovePlaceHolder()

			end
		elseif input.KeyCode == Enum.KeyCode.R then
			Rotation += 90
		end
	end
end)

Run.RenderStepped:Connect(function()
	if NewTower then
		local result = MouseRaycast({NewTower})
		if result and result.Instance then
			for i,v in pairs(workspace.Towers:GetChildren()) do
				PlcdTwrsHitbox = v.Hitbox
				PlcdTwrsHitbox.Transparency = 0.5
			end
			if result.Instance.Parent.Name == "TowerArea" then
				CanPlace = true
				ColorPlaceHolder(Color3.new(0, 1, 0))
			else
				CanPlace = false
				ColorPlaceHolder(Color3.new(1, 0, 0))
			end
			NewTower:SetPrimaryPartCFrame(CFrame.new(result.Position.X,result.Position.Y+(NewTower.Torso.Size.Y/2)+NewTower["Left Leg"].Size.Y,result.Position.Z)*CFrame.Angles(0,math.rad(Rotation),0))
		end
	else
		local result = MouseRaycast({NewTower})
		if result and result.Instance then
			if result.Instance.Parent.Parent.Name == "Enemies" then
				local MousePos = UserInput:GetMouseLocation()
				Gui.EnemyHealthGui.Position = UDim2.fromOffset(MousePos.X,MousePos.Y)
				Gui.EnemyHealthGui.Visible = true
				HlthModule.UpdateHealth(Gui.EnemyHealthGui,result.Instance.Parent)
			else
				Gui.EnemyHealthGui.Visible = false
			end
		else
			Gui.EnemyHealthGui.Visible = false
		end
	end
end)

Please ignore the unrelated part.
-The module

local Players = game:GetService("Players")
local LocalPlyr = Players.LocalPlayer
local Gui = LocalPlyr.PlayerGui.ScreenGui
local Map = workspace:WaitForChild("Test")
local Run = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local Camera = workspace.CurrentCamera
local function MouseRaycast()
	local MousePos = UserInput:GetMouseLocation()
	local MouseRay = Camera:ViewportPointToRay(MousePos.X,MousePos.Y)
	local RycstParams = RaycastParams.new()
	
	local RycstResult = workspace:Raycast(MouseRay.Origin,MouseRay.Direction*1000,RycstParams)
	return RycstResult
end	

local Health = {}

function Health.BaseSetup(health)
    	Health.BaseHlthGui = Gui.Topbar.Left.BaseHealthGui
		Health.BaseHealth = health
	    Health.MaxBaseHealth = health
	    Health.BaseModel = Map:WaitForChild("Base")
	
		Health.UpdateHealth(Health.BaseHlthGui,Health.BaseModel)
		Health.BaseModel.Humanoid.HealthChanged:Connect(function()
			Health.UpdateHealth(Health.BaseHlthGui,Health.BaseModel)
end)
end
function Health.UpdateHealth(gui,model)
	local Humanoid = model:WaitForChild("Humanoid")
	if Humanoid then
		gui.CurrentHealth.Size = UDim2.new(Humanoid.Health/Humanoid.MaxHealth,0,gui.CurrentHealth.Size.Y.Scale,0)

		if Humanoid.Health <= 0 then
			gui.HealthDisplay.Text = "0/"..Humanoid.MaxHealth
		else
			gui.HealthDisplay.Text = Humanoid.Health.."/"..Humanoid.MaxHealth	
		end	
		local NameDisplay = gui:FindFirstChild("NameDisplay")
		if NameDisplay then
				gui.NameDisplay.Text = model.Name
		end
	end

	end
	
return Health

Screenshots

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Does no one really help me with this?

Should be something with the :WaitForChild
I’ve had the same problem and if there is no child found it won’t run (it will wait forever)

1 Like