I’m a little lost in this thread, but heading back to the original problem, you said the value keeps jumping back up correct? I presume the problem is related to this line of code being ran in a local script.
You need to change datastore values via the server and not the client. This is because it is local (in the name), while changing on the server will be a global change. I suggest finding some way to notify the server for when a user touches the door and change the value through a server script.
Ok thanks, but whenever I used a remote event it says Value is not a valid member of Player. Im presume this is becuase I am using local script values, but Im not really sure how I would go about this.
local repStor = game:GetService("ReplicatedStorage")
local PointSubtraction = repStor:WaitForChild("PointSubtraction")
PointSubtraction.OnServerEvent:Connect(function(plr, door)
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - door.Value.Value
end)
I don’t think a remote event is actually necessary for this. You can use the Touched event via a server script and grab the player based on the parameter it provides.
I copied the script over, but it only runs once and can never be runned again.
local Workspace = game:GetService("Workspace")
local door = Workspace.Door
door.Touched:Connect(function(hit)
local human = hit.Parent:FindFirstChild("Humanoid")
if human ~= nil then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr.leaderstats.Points.Value >= door.Value.Value then
script.Enabled = false
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - door.Value.Value
end
end
end)
I assume that was intended since you disable the script here. Some stuff to take note of though.
1.) Check to make sure whatever touches the door is a player
2.) I would destroy the door rather than disabling the script if that is possible (through the client)
3.) Since you have multiple doors, instead of putting this script into each one, use a for loop.
game.Players.PlayerAdded:Connect(function(p)
local leaderstats = Instance.new("IntValue", p)
leaderstats.Name = "leaderstats"
local wins = Instance.new("IntValue", leaderstats)
wins.Name = "Wins"
local Points = Instance.new("IntValue", leaderstats)
Points.Name = "Points"
local x = wins.Value
Points.Value = 0
while true do
Points.Value += 1 + wins.Value
task.wait(1)
end
end)
game.Workspace.Door.Touched:Connect(function(hit)
if hit.Parent and hit.Parent:FindFirstChild("HumanoidRootPart") then
local value = door.Value
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local leaderstats = player.leaderstats
local points = leaderstats.Points
if points.Value - value.Value >= 0 then
points.Value -= value.Value
game.ReplicatedStorage.[eventname]:FireClient(player)
end
end
end)
local script:
local door = game.Workspace.door
game.ReplicatedStorage.[eventname].OnClientEvent:Connect(function()
door.Transparency = 0.5
door.CanCollide = false
wait(1)
door.Transparency = 0
door.CanCollide = true
end)
You can add in a bool value named open and then if it is false on the server side all it does is set it to true fires the event waits 1.1 seconds and sets it to false
for i,v in pairs(game.Workspace.Doors:GetChildren()) do
v.Touched:Connect(function(hit)
--continue the code for the touched event in here
-- when firing the event after the player do the door itself so v
end)
end
in the local script just replace the door variable with the door model you gave in the evemt
Im not sure how to send the variable over lol. I tried used an arguement to send v over when I fire the remote event, but it says it has to be a player.