How to Script Points/Guesses

I’m new at scripting and this is a script I have tried looking tutorials up of and constantly skimming the dev forum to find something to help but I have honestly found nothing. I’m scripting a guessing game which I have done the script for already but the only thing I am stuck on is getting points with every guess. Since its a chatted function, I have no clue how to go about this. I have two different components which is guessing and points. I’m trying to script it where you guess one right, you get one guess “point” and then you get two points that are separate from the guesses. Here is my leaderstats that I made:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent =player
	
	local Guesses = Instance.new("NumberValue")
	Guesses.Name = "Guesses"
	Guesses.Parent = leaderstats
	Guesses.Value = 0
	
	local Points = Instance.new("NumberValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	Points.Value = 0
end)

Here is my guessing script that I have for each door instance:

door = script.Parent
function onChatted(msg, recipient, speaker) 
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	local thecharacter = script.Parent.TheCharacter
	local decal = script.Parent.Decal ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
	if (msg == string.lower(thecharacter.Value)) then 
		door.CanCollide = false 
		door.Transparency = 0.7 
		decal.Transparency = 0.7 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT) 
		wait(2) ----------- How long you need to wait for the door to be visible again 
		decal.Transparency = 0 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
		door.CanCollide = true 
		door.Transparency = 0 
	end 
end 
game.Players.ChildAdded:connect(function(plr)
	plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)

If anyone could point me in a certain direction or provide me with any sort of help that would be immensely appreciated! Thanks in advance.

1 Like

Could you show me the part where it says where you guess one right?

Anyways you could just do this,

Here is an example to help you out:

player.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1

It was actually just those two scripts that I provided mushed together and I added something similar to what you suggested but it didnt seem to work.

I don’t think you added it to the right spot. I’m assuming

is the player.

so just add this line of code

door = script.Parent
function onChatted(msg, recipient, speaker) 
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	local thecharacter = script.Parent.TheCharacter
	local decal = script.Parent.Decal ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
    recipient.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1 -- Adds guess value
	if (msg == string.lower(thecharacter.Value)) then 
		door.CanCollide = false 
        recipient.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 -- Adds Points valu
		door.Transparency = 0.7 
		decal.Transparency = 0.7 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT) 
		wait(2) ----------- How long you need to wait for the door to be visible again 
		decal.Transparency = 0 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
		door.CanCollide = true 
		door.Transparency = 0 
	end 
end 
game.Players.ChildAdded:connect(function(plr)
	plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)

Edit: If its not then it may be speaker

replace recipient with speaker for the 2 new lines i added

1 Like
function onChatted(msg, recipient, speaker) 
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	local thecharacter = script.Parent.TheCharacter
	local decal = script.Parent.Decal ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
    speaker.leaderstats.Guesses.Value = player.leaderstats.Guesses.Value + 1 -- Adds guess value
	if (msg == string.lower(thecharacter.Value)) then 
		door.CanCollide = false 
        speaker.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 -- Adds Points value
		door.Transparency = 0.7 
		decal.Transparency = 0.7 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT) 
		wait(2) ----------- How long you need to wait for the door to be visible again 
		decal.Transparency = 0 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
		door.CanCollide = true 
		door.Transparency = 0 
	end 
end 
game.Players.ChildAdded:connect(function(plr)
	plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)

Fixed. It should work

2 Likes

My leaderstats is in the serverscriptstorage and the script for the guessing door is in the workplace. Do I need to move the script somewhere else? Because as of right now the player in that script is counted as an unknown global.

Could you share me the full error? Thanks

function onChatted(msg, recipient, speaker) 
	local source = string.lower(speaker.Name) 
	msg = string.lower(msg) 
	local thecharacter = script.Parent.TheCharacter
	local decal = script.Parent.Decal ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
    speaker.leaderstats.Guesses.Value = speaker.leaderstats.Guesses.Value + 1 -- Adds guess value
	if (msg == string.lower(thecharacter.Value)) then 
		door.CanCollide = false 
        speaker.leaderstats.Points.Value = speaker.leaderstats.Points.Value + 1 -- Adds Points value
		door.Transparency = 0.7 
		decal.Transparency = 0.7 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT) 
		wait(2) ----------- How long you need to wait for the door to be visible again 
		decal.Transparency = 0 ----- Delete THIS LINE ONLY if you DONT have a DECAL. (IMPORTANT)
		door.CanCollide = true 
		door.Transparency = 0 
	end 
end 
game.Players.ChildAdded:connect(function(plr)
	plr.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, plr) end) 
end)

Nevermind i saw the problem. Try again and see it should work, if it doesn’t send me the full error.

2 Likes

Scripts don’t run in ServerScriptStorage. You should move both scripts to ServerScriptService.

Edit: And you don’t want your ServerScripts in workspace because they are replicated to the client and can be stolen by exploiters.

1 Like

Thanks so much! It worked. The way I was doing it before clashed with my leaderstats which is why I think I was having that problem. This really cleared things up for me. Thanks again for your help!

I have a script attached to a part. Would it still work if I moved it to the ServerScriptStorage?

As long as you change the path. Instead of script.Parent, It would be something like game.Workspace.Part.

1 Like

I didn’t know things like that could happen! I have so many scripts in my workspace that are like that. Thanks for the heads up and the help! I’ll go and fix some things right now.

1 Like

Doesn’t always have to be that way. It really depends on people’s preferences and also depends on how you sort your workspace out.

Just realized you said ServerScriptStorage. Make sure you put it in ServerScriptService if you want it to run. ServerScriptStorage would be good for storing Objects.

Depends if you want to chance your scripts being stolen or not. If you plan on your game becoming popular, you’ll want to do everything you can to make sure your scripts are secure.

1 Like

Oops sorry! I totally meant to say Service not Storage. I get those two mixed up sometimes.

1 Like

If it’s a server script you don’t have to worry anything about it since those scripts can only be seen by the server. I guess your right though.

1 Like

By the sound of Server Script, you’d think so. But they’re actually replicated to the client if they’re in Workspace, ReplicatedStorage, or a few others though.

Edit: Looking into it, it doesn’t seem to be as replicated / easily stealable as I was thinking. Not too sure honestly. Don’t take my word for it lol.

1 Like

This isn’t true. Server scripts are never sent to the client, so they’re safe to be in workspace. The only thing the client knows is that they exist(and the most they can do to them is delete them if they’re in their character, e.g. the default health script). However, it’s better practice to use ServerScriptService unless you’re using a script.Parent connection to something.

1 Like

So I went through and replaced the scripts for each door only to play through and realize that something is wrong with the guesses. I guess one door and it ends up, I believe, playing through all the scripts and giving me a lot of points.
example2
As seen here, this is just with one guess. I get 586 extra guesses. The points are correct for each one but I have no idea why its giving me those many guesses.
example
This is where the script is in the Workplace. And I have the same script for all the doors in the workplace. I don’t see why the points work perfectly but the guesses don’t.

Edit: Should I use a FireDoorClient in ReplicatedStorage to connect the door opening script to a new guess/point leaderstats in the serverscriptservice? I did something like that for something else and it worked fine. Just not sure what else to do.

Edit2: Just realized that the guess function doesn’t work at all. I chat random things and it gives me guessing “points”. The points function is the only one that works. Idk what to do.