Sending a server message in a ServerScript

Hey there,
I have a script that gives the player 1 leaderstat when it is touched but I am also trying to make it say in the chat “[Server]: Player just won!” But when I try and do it in the server script it says "StarterGui:SetCore must be called from a local script but when I try my script as a local script it wont give the player 1 leaderstat nor send the message.

Script:

amnt = 1
local debounce = false
function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	local plr = part.Parent
	if (h~=nil) then
		local thisplr = game.Players:findFirstChild(h.Parent.Name)
		if (thisplr~=nil) then
			local stats = thisplr:findFirstChild("leaderstats")
			if (stats~=nil) then
				local score = stats:findFirstChild("Wins")
				if (score~=nil) then
					if not debounce then
						debounce = true
						score.Value = score.Value + amnt
						wait(60)
						debounce = false
					end
				end
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)

Chat message:

game.StarterGui:SetCore( "ChatMakeSystemMessage",  { Text = "[Server]: "..plr.Name.. " just won!", Color = Color3.fromRGB(255, 6, 51), Font = Enum.Font.Arial, FontSize = Enum.FontSize.Size24 } )
1 Like

You could use FireAllClients() to fire every client and handle the message being shown inside of a local script that way, keep the leaderstats handling in the server script.

2 Likes

thank you ill give that a shot!

would i use a RemoteEvent to do that?

Yes, a RemoteEvent would be required.

1 Like

Alright thank you, so if I put the leaderstat thing and the chat message in 2 different scripts how would I get that the player touched the part in the 2nd script?

Hey Im just getting an error after the same person touches the part twice.

13:43:45.201 Workspace.WinPart.Script:25: invalid argument #2 to ‘remove’ (number expected, got Instance) - Server - Script:25

If you share the script I’ll take a look.

1 Like

here you go

local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
local playerDebounce = {}

function onTouched(part)
	if part.Parent:FindFirstChild("Humanoid") then
		local plr = players:GetPlayerFromCharacter(part.Parent)
		local char = plr.Character
		local hum = char:WaitForChild("Humanoid")
		if plr then
			local leaderstats = plr:WaitForChild("leaderstats")
			if leaderstats then
				local score = leaderstats:WaitForChild("Wins")
				local Credits = leaderstats:WaitForChild("Credits")
				if score then
					if table.find(playerDebounce, plr) then
						return
					end
					table.insert(playerDebounce, plr)
					wonEvent:FireAllClients(plr)
					score.Value += 1
					Credits.Value = Credits.Value + math.random(75, 100)
					task.wait(60)
					table.remove(playerDebounce, plr)
				end
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local wonEvent = repStorage:WaitForChild("Events"):WaitForChild("Won")
local playerDebounce = {}

function onTouched(part)
	if part.Parent:FindFirstChild("Humanoid") then
		local plr = players:GetPlayerFromCharacter(part.Parent)
		local char = plr.Character
		local hum = char:WaitForChild("Humanoid")
		if plr then
			local leaderstats = plr:WaitForChild("leaderstats")
			if leaderstats then
				local score = leaderstats:WaitForChild("Wins")
				if score then
					if table.find(playerDebounce, plr) then
						return
					end
					table.insert(playerDebounce, plr)
					wonEvent:FireAllClients(plr)
					score.Value += 1
					task.wait(60)
					for i, player in pairs(playerDebounce) do
						if player == plr then
							table.remove(playerDebounce, i)
						end
					end
				end
			end
		end
	end
end

script.Parent.Touched:connect(onTouched)