Server Invoking

My current Obby, with help, We created an “OnServerInvoke” GUI button which is triggered by a player standing on the last checkpoint. You then press the button and it transports you back to the beginning.

However, I no longer want it to activated by a button. I want it just happen when they stand on the last checkpoint. I tried to link it to an ontouch event but it failed. Any help understanding how to do it is appreciated. :slight_smile:

This is the script on the Leaderstats page

game.ReplicatedStorage.Prestige.OnServerInvoke = function (player)
	if player.leaderstats.Node.Value == 42 then
		player.leaderstats.Prestige.Value += 1
		player.leaderstats.Node.Value = 0
		

		player:LoadCharacter()
	end
end 

This code I have on touch, when I touch the last checkpoint

local part = script.Parent

part.Touched:Connect(function(hit) -- this event fires whenever something touches the part
	if hit.Parent:FindFirstChild("Humanoid") then -- if humanoid exists
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- gets the player from the character

		if player then -- if player exists
			if not player.PlayerGui.ScreenGuiRB.PrestigeButton.Visible then 
				-- change the 'ScreenGui.Frame' to the actual name of your ScreenGui and the Frame (or it can be TextLabel or ImageLabel etc)
				player.PlayerGui.ScreenGuiRB.PrestigeButton.Visible = true
			end
		end
	end
end)

This is the code in the GUI

local player = game.Players.LocalPlayer
local PrestigeButton = script.Parent.PrestigeButton
local replicatedStorage = game:WaitForChild("ReplicatedStorage")

PrestigeButton.MouseButton1Up:Connect(function()
	replicatedStorage.Prestige:InvokeServer()
end)

s4

Put unique print() statements inside every “if” statement, this will likely identify which condition is stopping it from working.

2 Likes

I get a ServerInvoke is only available from the client… Okay, I fixed that by making it a local script. This is the script so far.

local part = script.Parent

part.Touched:Connect(function(hit) -- this event fires whenever something touches the part
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local playerName = hit.Parent.Name
		local Prestige = game.ReplicatedStorage.Prestige:InvokeServer()

	end
end)



Return the Touched script to serverside and update the touched function as follows:

local part = script.Parent

part.Touched:Connect(function(hit) 
	if hit.Parent:FindFirstChild("Humanoid") then 
		local player = game.Players:GetPlayerFromCharacter(hit.Parent) 

		if player then
			if player.leaderstats.Node.Value == 42 then
				player.leaderstats.Prestige.Value += 1
				player.leaderstats.Node.Value = 0


				player:LoadCharacter()
			end
		end
	end
end)