I'm trying to make this script that allows players to capture bases which, depending on the type of base, will give them benefits.
I'm trying to make it so that when the player stands on the brick that this script is inserted into, the base gets captured, and also, if the player decides to leave for some reason, the capture process is cancelled. In Studio, the script works perfectly fine. However, when I play in the server, it doesn't work at all.
I've tried to do numerous things, such as using wait() and converting it to a LocalScript, but nothing has changed. I even tried checking Output, which had no response.
"Xaggis" means that the base belongs to the enemy NPCs whereas N/A is Unafilliated, and "Steelgarde" means that the player owns it.
Here's the code:
function onTouched(part) Capture = script.Parent wait(1) local human = game.Players.LocalPlayer.Character:findFirstChild("Humanoid") while true do wait(2) Distance = (game.Players.LocalPlayer.Character.Torso.Position - Capture.Position).magnitude if Distance <= 20 then if script.Parent.Parent.Owner.Value == "Xaggis" then --If owned by NPCs/Enemies/unaffiliated, capture base script.Parent.Parent.CapturePt.CaptureWhite.Enabled = true wait(5) if script.Parent.Parent.Owner.Value == "Xaggis" then --If owned by NPCs/Enemies, make base Unaffiliated script.Parent.Parent.Owner.Value = "N/A" --The value is unaffiliated end end if script.Parent.Parent.Owner.Value == "N/A" then script.Parent.Parent.CapturePt.CaptureBlue.Enabled = true wait(5) script.Parent.Parent.CapturePt.CaptureBlue.Enabled = false if script.Parent.Parent.Owner.Value == "N/A" then script.Parent.Parent.Owner.Value = "Steelgarde" -- Base captured by players! wait(1) script.Parent.Parent.CapturePt.CaptureWhite.Enabled = false end end end if Distance >= 21 then if script.Parent.Parent.Owner.Value == "Xaggis" then script.Parent.Parent.Owner.Value = "Xaggis" script.Parent.Parent.CapturePt.CaptureWhite.Enabled = false end if script.Parent.Parent.Owner.Value == "N/A" then script.Parent.Parent.Owner.Value = "N/A" script.Parent.Parent.CapturePt.CaptureBlue.Enabled = false else end end end end script.Parent.Touched:connect(onTouched)
I am not 100 percent sure this will work, but I have fixed a few of the problems at hand with this script.
Firstly, I would like to strongly recommend you try to better indent and organize your if statements
in particular. Being able to read the script is important, especially if you end up making scripts that push 1000 lines.
You do not want to use a local script in this case because of where the script is being placed and what it is doing. Local scripts typically want to be in the player somehow (I typically use the StarterGui).
Read through this code I have to make sure I haven't messed up your if statements by trying to make them more legible.
--Server script local Base = script.Parent.Parent --Makes it easier to read function onTouched(hit) wait(1) if hit.Parent:FindFirstChild("Humanoid") then --This is a better way to find a humanoid with the Touched function local human = hit.Parent.Humanoid local torso = hit.Parent:FindFirstChild("Torso") --Don't use local player to get this in this script. while wait() do Distance = (torso.Position - Capture.Position).magnitude if Distance <= 20 then if Base.Owner.Value == "Xaggis" then --If owned by NPCs/Enemies/unaffiliated, capture base Base.CapturePt.CaptureWhite.Enabled = true wait(5) if Base.Owner.Value == "Xaggis" then --If owned by NPCs/Enemies, make base Unaffiliated Base.Owner.Value = "N/A" --The value is unaffiliated end if Base.Owner.Value == "N/A" then Base.CapturePt.CaptureBlue.Enabled = true wait(5) Base.CapturePt.CaptureBlue.Enabled = false end if Base.Owner.Value == "N/A" then Base.Owner.Value = "Steelgarde" -- Base captured by players! wait(1) Base.CapturePt.CaptureWhite.Enabled = false end end end if Distance >= 21 then if Base.Owner.Value == "Xaggis" then Base.Owner.Value = "Xaggis" Base.CapturePt.CaptureWhite.Enabled = false end if Base.Owner.Value == "N/A" then Base.Owner.Value = "N/A" Base.CapturePt.CaptureBlue.Enabled = false else --empty end end end end end script.Parent.Touched:connect(onTouched)
If I happen to have helped answer your question, please remember to accept my answer. If my answer is incorrect please comment on my answer or message me via Roblox so I can try to fix my mistakes or edit any simple mistakes. Thanks!