I’m very new to scripting and this is my first time using these services. Whenever I join the game to test if I get the badge on VR it doesn’t work, what’s up with the code?
local VRService = game:GetService("VRService") --service call
local userInputService = game:GetService("UserInputService") --service call
local BadgeService = game:GetService("BadgeService") --service call
local Players = game:GetService("Players") --service call
local isUsingVR = userInputService.VREnabled --checks if player is using VR
if (isUsingVR) then
BadgeService:AwardBadge(Players.UserId, 0000000000) --awards badge
end
local VRService = game:GetService("VRService") --service call
local userInputService = game:GetService("UserInputService") --service call
local BadgeService = game:GetService("BadgeService") --service call
local Players = game:GetService("Players") --service call
local isUsingVR = userInputService.VREnabled --checks if player is using VR
if (isUsingVR) then
BadgeService:AwardBadge(Players.UserId, 0000000000) --awards badge
end
I found a few problems in this script, if you have not already change the 000000000000 to the badge Id you find on the website page.
Either way, you probably have. So here is the main problem.
if (isUsingVR) then
Remove the () and put it in carrots, either way you dont have to put it in carrots.
also, change
local Players = game:GetService("Players")
to
local Players = game:GetService("Players").LocalPlayer
Because it cant access the UserId if it’s not a local player. Also it depends where this script is in explore. So there is alot of problems.
Sorry I didn’t get to you until now. My inbox was full of notifications. I hid the badge ID because I don’t want people to know what game it is quite yet. I have the script as a local script stored in workspace, does this need to be stored in a certain place like StarterPlayerScripts or such?
Update: putting the now edited script in StarterPlayerScripts does nothing.
You cannot check from the server whether or not a player is using a VR headset. The check has to be performed on the client, which will have to inform the server so that it can award the player the badge.
This is insecure - a clever exploiter could easily trick the server into giving them the badge even if they don’t have a VR headset. You will have to accept this vulnerability if you want to add this badge to your game: there is no safe way to check if a player is VR-enabled.
Here is an example setup using a RemoteEvent:
-- Server code: ServerScriptService.VRBadgeScript
local BADGE_ID = 000000
local REMOTE_NAME = "VRBadgeRemote"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BadgeService = game:GetService("BadgeService")
local Remote = Instance.new("RemoteEvent")
Remote.OnServerEvent:Connect(function(Player)
BadgeService:AwardBadge(Player.UserId,BADGE_ID)
end)
Remote.Name = REMOTE_NAME
Remote.Parent = ReplicatedStorage
-- Client code: StarterCharacterScripts.VRBadgeScript
local REMOTE_NAME = "VRBadgeRemote"
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Remote = ReplicatedStorage:WaitForChild(REMOTE_NAME,5)
if UserInputService.VREnabled and Remote then
Remote:FireServer()
end
Thanks so much! It’s ok that it’s somewhat vulnerable, the in-game reward you receive from this isn’t huge enough to worry about. However this should be accounted for by anyone looking at this script in the future!