Why is the weapon code not working?

According to the idea, ClipSize (The number of the magazines) has a MaxValue and Value and should change on the server and on the client, but it changes only on the client, or only on the server.

Configuration.Ammo.ClipSize.Value = Configuration.Ammo.ClipSize.Value - 1
script.Parent.rld:FireServer()
script.Parent.rld.OnServerEvent:Connect(function()
	wait(0.2)
	script.Parent.Configuration.Ammo.ClipSize.Value = script.Parent.Configuration.Ammo.ClipSize.Value - 1
	script.Parent.Handle.MagOut:Play()
	wait(1.3)
	script.Parent.Handle.MagIn:Play()
end)
1 Like

Try replacing

script.Parent.Configuration.Ammo.ClipSize.Value = script.Parent.Configuration.Ammo.ClipSize.Value - 1

with

script.Parent.Configuration.Ammo.ClipSize.Value -= 1

I know they probably do the same thing, but its cleaner too look at

1 Like

I checked it doesn’t work it only changes on the server

Can you share a video of the issue?

Screenshot_23
The difference between the maximum and the current ClipSize value
Screenshot_24
The number of ClipSize in the interface (the value is checked, there are three)
(I have now changed the code)

script.Parent.rld.OnServerEvent:Connect(function()
	wait(0.2)
	script.Parent.Configuration.Ammo.ClipSize.Value -= 1
	script.Parent.Handle.MagOut:Play()
	wait(1.3)
	script.Parent.Handle.MagIn:Play()
end)
script.Parent.rld:FireServer()

The value is replicated to the server and does not need to be subtracted on the client. And if this just fires when the script runs, modules and connections might not be ready to run yet, so nothing happens.

And I just changed the script, now it only changes on the server, but the client ignores it…

What is the client ignoring? The value is changed on both sides, but printing the value right after might not work because of delay. Try waiting a little and checking its value. Wait any amount.

Also why is this firing? If it’s on click that can be detected on the server. I need code for both scripts.

Yes, by clicking on R. I will throw off the entire function, as well as the remoteevent script.

function Reload()
	if Reloading or not Tool.Enabled or Configuration.Ammo.Magazines.Value >= Configuration.Ammo.Magazines.MaxValue then
		return
	end
	Tool.Enabled = false
	Reloading = true
	ToggleGui()
	local CanReload = true
	if Configuration.Ammo.ClipSize.MaxValue > 0 and Configuration.Ammo.ClipSize.Value <= 0 then
		CanReload = false
	else
		CanReload = true
	end
	if CanReload then
		Spawn(function()
			local Animation = Animations.Reload
			OnClientInvoke("StopAnimation", Animations.Hold)
			OnClientInvoke("PlayAnimation", Animation)
		end)
		script.Parent.rld:FireServer()
		Sounds.Reload:Play()
		AddedClips = Configuration.Ammo.Magazines.MaxValue - Configuration.Ammo.Magazines.Value
		Configuration.Ammo.Magazines.Value = Configuration.Ammo.Magazines.MaxValue
		wait(Animations.Reload.Duration*0.93)
		OnClientInvoke("PlayAnimation", Animations.Hold)
		Sounds.Reload:Stop()
		ToggleGui()
	end
	Reloading = false
	Tool.Enabled = true
end
script.Parent.rld.OnServerEvent:Connect(function()
	wait(0.2)
	script.Parent.Configuration.Ammo.ClipSize.Value -= 1
	script.Parent.Handle.MagOut:Play()
	wait(1.3)
	script.Parent.Handle.MagIn:Play()
end)

There will be a 0.2 delay You have to wait for 0.21 seconds for guaranteed value being set on the client

After this time, it changes only on the server. That is, when I take an ammo box, there is a difference between the current value and the maximum value, after which the found difference is added to this current value, and this only happens on the server, no changes were found on the client.

I checked the value changes, it is changing on the client and on the server, but when the ClipSize value is less than or equal to zero, the weapon still shoots, although when I processed it on a local script, everything was normal.

I am quite confused here, the client should mostly always handle stuff responsible for the user’s interface. The server handles the actual functionality. This is for replication and so that it is protected from hackers. I don’t quite understand this. Can I have a little bit of the shooting function so I know what’s happening? I want the function from both sides, if the server handles it, it’s because you handled some info only on the client.

The whole point was that the script created a new Configuration and added it to the table, and it does not relate to the weapon Configuration and accordingly did not change.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.