Help With Goal System

I am trying to make a goal system. The script works fine once weather the OrangeHitBox or BlueHitBox is touched by the part but doesn’t work on the other one if the other part was touched first. When I use the Print Debug method it prints the first time but not when the other one it touched. Also it only works once. How do I fix this

-- SERVER SCRIPT LOCATIED IN GOALALERT
-- <<VERIABLES>>
local MainFrame = script.Parent:WaitForChild("MainFrame")
local Text_Red = MainFrame:WaitForChild("Red")
local Text_Blue = MainFrame:WaitForChild("Blue")
local part = workspace:WaitForChild("partA")

-- <<FUNCTIONS>>
local function onHit(hit)
	if hit.Name == "OHitBox" then
		MainFrame.Visible = true
		Text_Blue.Visible = true
	end

	if hit.Name == "BHitBox" then
		MainFrame.Visible = true
		Text_Red.Visible = true
	end

	task.wait(2)
	MainFrame.Visible = false
	Text_Red.Visible = false
	Text_Blue.Visible = false
end

-- <<CONNECTIONS>>
part.Touched:Connect(onHit)

also this script is in StarterGui

2 Likes

Try to put this:

if hit:IsA("BasePart") and hit.Name == "OHitBox" then

if hit:IsA("BasePart") and hit.Name == "BHitBox" then

Thanks, but when I did that nothing happened

I have 2 goals per map and I have five maps. They easiest way to fix this is by adding a script to each of them and when they are touched the UI comes up.

But the other way I do not know how to do it. It would be to get all of the goals and make one script that detects when one of them are hit and the UI comes Up. But I do not know how to do this. I prefer to do it this way

Then try replacing the script with a localscript then make a RemoteEvent and call it HitEvent then put it in replicated storage. Then, put a script in SSS (ServerScriptService) Next replace the function in the LocalScript with this:

-- keep the part variable but delete the other variables...

function onHit(hit)
	game.ReplicatedStorage.HitEvent:FireServer(hit)
end

part.Touched:Connect(onHit)

For the serverscript, do:

local event = game.ReplicatedStorage.HitEvent

event.OnServerEvent:Connect(function(ply,hit)
	if hit:IsA("BasePart") and hit.Name == "OHitBox" then
		-- code here
	end
	if hit:IsA("BasePart") and hit.Name == "BHitBox" then
		-- code here
	end
end)

If that didn’t work or printed out an error please send back a message

you don’t really need this… what other objects are called “BHitBox”

edit: i meant the if hit:IsA(“BasePart”)

What do you mean i dont need it?

Well get rid of that part of the if statement if it isn’t useful then.

@Cyber_Designer what does this print?

-- SERVER SCRIPT LOCATIED IN GOALALERT
-- <<VERIABLES>>
local MainFrame = script.Parent:WaitForChild("MainFrame")
local Text_Red = MainFrame:WaitForChild("Red")
local Text_Blue = MainFrame:WaitForChild("Blue")
local part = workspace:WaitForChild("partA")

-- <<FUNCTIONS>>
local function onHit(hit)
	if hit.Name == "OHitBox" then
		MainFrame.Visible = true
		Text_Blue.Visible = true
		print("orange")
	elseif hit.Name == "BHitBox"
		MainFrame.Visible = true
		Text_Red.Visible = true
		print("blue")
	else 
		print("something else")
	end

	task.wait(2)
	MainFrame.Visible = false
	Text_Red.Visible = false
	Text_Blue.Visible = false
end

-- <<CONNECTIONS>>
part.Touched:Connect(onHit)

Well if it hits anything else besides the orange hitbox it will just do the blue hitboxes part even if it doesnt touch the blue hitbox

edit: or in this case “BHitBox”

When I tried this nothing happened, but it gave me an idea (that i don’t know how to do). What you gave me was a remote event that fired the event. I have another script that destroys the part when it touches the HitBoxes.

My idea is fire a remote event from the other script and so forth but I don’t know how to fire a remote event on server side to server side

If you want to fire RemoteEvents on server to server you have to use whats instead called BindableEvents

They’re similar to RemoteEvents however you use .Event instead of .OnServerEvent and :Fire() instead of FireServer()

One other thing that should be noted is that bindableevents cant fetch the player because… well… its a server to server thing and its not locally

Would it be better to make the server fire to local

Im pretty sure you dont even need a script in the screengui. Just put it somewhere in serverstorage and remove the remoteevent and local script and paste you’re original code (from where you first started this post) and put it in the serverscript and modify some things around

1 Like

my bad put the serverscript in SSS

Ok I got it down to the script below. The problem is that I the UI stays up for a very long time and then goes a way. I want it to wait 3 seconds and the disappear. How do I fix this

local part= script.Parent

local function showUI(frame)
    for _, Player in pairs(game.Players:GetChildren()) do
        Player.PlayerGui.GoalAlert.MainFrame.Visible = true
        Player.PlayerGui.GoalAlert.MainFrame[frame].Visible = true
        wait(3) 
        Player.PlayerGui.GoalAlert.MainFrame.Visible = false
        Player.PlayerGui.GoalAlert.MainFrame[frame].Visible = false
    end
end

local function oHitBox()
    showUI("Blue")
end

local function bHitBox()
    showUI("Red")
end

part.Touched:Connect(function(hit)
    if hit.Name == "OHitBox" then
        oHitBox()
    elseif hit.Name == "BHitBox" then
        bHitBox()
    end
end)

put task.wait() instead of wait(). Its better and im pretty sure it boosts performance

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.