英文:
Why can i not place my towers down (roblox)
问题
我理解,以下是翻译好的部分:
我正在制作一个塔防游戏,我正在修改我的脚本,以使在放置塔时必须再次按下按钮才能放置另一个塔,然后我开始工作在有效和无效放置区域上。一旦我完成了它,我进行了测试,但出现了一个关于我的 then 和 end 语句之一的错误。我尝试修改它,直到指示错误的红线消失,但然后当我测试时,按钮停止工作,但我没有收到任何错误消息。我将脚本恢复为以前的状态,我的 ends 下有红线,现在我不知道该怎么办。这是脚本,我遇到问题的部分在底部。这是我收到的错误消息:"Players.ASWXDYC2.PlayerGui.ScreenGui.GameController:143: 预期 'end'(在第 48 行关闭 'function'),但获得
这是脚本:
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("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
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)```
</details>
# 答案1
**得分**: 1
所以基本上问题并不是我在我的"renderstepped"部分没有足够的"ends"。我需要在我的"ColorPlaceHolderTower"函数中再添加一个"end"。
<details>
<summary>英文:</summary>
So basically the problem wasnt that i didnt have enough "ends" in my renderstepped part. I had to add another end in my ColorPlaceHolderTower function.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论