为什么我不能放置我的塔(roblox)

huangapple go评论78阅读模式
英文:

Why can i not place my towers down (roblox)

问题

我理解,以下是翻译好的部分:

我正在制作一个塔防游戏,我正在修改我的脚本,以使在放置塔时必须再次按下按钮才能放置另一个塔,然后我开始工作在有效和无效放置区域上。一旦我完成了它,我进行了测试,但出现了一个关于我的 then 和 end 语句之一的错误。我尝试修改它,直到指示错误的红线消失,但然后当我测试时,按钮停止工作,但我没有收到任何错误消息。我将脚本恢复为以前的状态,我的 ends 下有红线,现在我不知道该怎么办。这是脚本,我遇到问题的部分在底部。这是我收到的错误消息:"Players.ASWXDYC2.PlayerGui.ScreenGui.GameController:143: 预期 'end'(在第 48 行关闭 'function'),但获得 ;你是否忘记在第 85 行关闭 'then'?

这是脚本:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local towerToSpawn = nil
local gui = script.Parent
local replicatedstorage = game:GetService("ReplicatedStorage")
local towers = replicatedstorage:WaitForChild("Towers")
local PhysicsService = game:GetService("PhysicsService")
local events = replicatedstorage:WaitForChild("Events")
local spawnTowerEvent = events:WaitForChild("SpawnTower")
local canPlace = false

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	return raycastResult
end

local function RemovePlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
	end
end

local function AddPlaceHolderTower(name)
	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers
	end
end

local function ColorPlaceholderTower(color)
	for i, object in ipairs(towerToSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color
		end
	end
end

gui.Gunslinger.Activated:Connect(function()
	AddPlaceHolderTower("Gunslinger")
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if towerToSpawn then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if canPlace then
				spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
				RemovePlaceholderTower()
			end
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if towerToSpawn then
		local result = MouseRaycast({towerToSpawn})
		if result and result.Instance then
			if result.Instance.Parent.Name == "TowerArea" then
				canPlace = true
				ColorPlaceholderTower(Color3.new(0,1,0))
			else
				canPlace = false
				ColorPlaceholderTower(Color3.new(1,0,0))
			end
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y * 2)
			local z = result.Position.Z
			local cframe = CFrame.new(x,y,z)
			towerToSpawn:SetPrimaryPartCframe(cframe)
		end
	end
end)
英文:

Im making a tower defense game and i was modifying my script to make it so that when u place a tower down u have to press the button again to place another one down then i started working on a valid and invalid placement areas. once i finished it i tested it and got an error with one of my then and end statements. I played around with it until the red lines indicating errors disappeared but then when i tested it the button stopped working but i didnt get any error messages. I turned the script back how it used to be with the red lines under my ends and now i dont know what to do. Here is the script, the part im having trouble with is the part at the bottom. this is the error message im getting " Players.ASWXDYC2.PlayerGui.ScreenGui.GameController:143: Expected 'end' (to close 'function' at line 48), got <eof>; did you forget to close 'then' at line 85? "

here is the script.

local RunService = game:GetService(&quot;RunService&quot;)
local UserInputService = game:GetService(&quot;UserInputService&quot;)
local camera = workspace.CurrentCamera
local towerToSpawn = nil
local gui = script.Parent
local replicatedstorage = game:GetService(&quot;ReplicatedStorage&quot;)
local towers = replicatedstorage:WaitForChild(&quot;Towers&quot;)
local PhysicsService = game:GetService(&quot;PhysicsService&quot;)
local events = replicatedstorage:WaitForChild(&quot;Events&quot;)
local spawnTowerEvent = events:WaitForChild(&quot;SpawnTower&quot;)
local canPlace = false


local function MouseRaycast(blacklist)
	
	local mousePosition = UserInputService:GetMouseLocation()

	local mouseRay = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)

	return raycastResult
end

local function RemovePlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
	end
end

local function AddPlaceHolderTower(name)
	
	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers

	end
end

local function ColorPlaceholderTower(color)

	for i, object in ipairs(towerToSpawn:GetDescendants()) do
		if object:IsA(&quot;BasePart&quot;) then
			object.Color = color
		end
end

gui.Gunslinger.Activated:Connect(function()
	AddPlaceHolderTower(&quot;Gunslinger&quot;)
	
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if towerToSpawn then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
				if canPlace then
					spawnTowerEvent:FireServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
					RemovePlaceholderTower()
				end
				

				
		end
	end
	
end)



RunService.RenderStepped:Connect(function()
	if towerToSpawn then
	local result = MouseRaycast({towerToSpawn})
		if result and result.Instance then
			if result.Instance.Parent.Name == &quot;TowerArea&quot; then
			canPlace = true
			ColorPlaceholderTower(Color3.new(0,1,0))
		else
			canPlace = false
			ColorPlaceholderTower(Color3.new(1,0,0))
		end
		local x = result.Position.X
		local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y * 2)
		local z = result.Position.Z
		
		local cframe = CFrame.new(x,y,z)
		towerToSpawn:SetPrimaryPartCFrame(cframe)
			
		end
	end
end)```

</details>


# 答案1
**得分**: 1

所以基本上问题并不是我在我的"renderstepped"部分没有足够的"ends"。我需要在我的"ColorPlaceHolderTower"函数中再添加一个"end"。

<details>
<summary>英文:</summary>

So basically the problem wasnt that i didnt have enough &quot;ends&quot; in my renderstepped part. I had to add another end in my ColorPlaceHolderTower function.

</details>



huangapple
  • 本文由 发表于 2023年6月12日 11:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定